Skip to content

Instantly share code, notes, and snippets.

@sag333ar
Created January 25, 2022 03:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sag333ar/9e7bf9010c6202f2f329a7cbc535d56b to your computer and use it in GitHub Desktop.
Save sag333ar/9e7bf9010c6202f2f329a7cbc535d56b to your computer and use it in GitHub Desktop.
Flutter - Dart - Get the type of the device based on device width.
import 'package:flutter/material.dart';
enum ScreenType {
desktop,
tablet,
handset,
watch,
}
class FormFactor {
static double desktop = 900;
static double tablet = 600;
static double handset = 300;
static ScreenType getFormFactor(BuildContext context) {
// Use .shortestSide to detect device type regardless of orientation
double deviceWidth = MediaQuery.of(context).size.width; // or .shortestSide
if (deviceWidth > FormFactor.desktop) return ScreenType.desktop;
if (deviceWidth > FormFactor.tablet) return ScreenType.tablet;
if (deviceWidth > FormFactor.handset) return ScreenType.handset;
return ScreenType.watch;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment