Created
January 25, 2022 03:53
-
-
Save sag333ar/9e7bf9010c6202f2f329a7cbc535d56b to your computer and use it in GitHub Desktop.
Flutter - Dart - Get the type of the device based on device width.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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