getFormFactor static method
Although specifying the BuildContext
is optional, providing it can prevent layout issues when this widget renders immediately after the app launch.
If this widget needs to be used at the moment the app launches, it's recommended to provide the BuildContext
here.
Implementation
static DeviceType getFormFactor([BuildContext? context]) {
if (deviceType != null) return deviceType!;
if (PlatformUtils().isWeb) {
final win = WidgetsBinding.instance.platformDispatcher.views.first;
final size = win.physicalSize;
final screenWidth = size.width / win.devicePixelRatio;
final screenHeight = size.height / win.devicePixelRatio;
final diagonalInInches =
sqrt(pow(screenWidth, 2) + pow(screenHeight, 2)) / 96.0;
deviceType = diagonalInInches < 11.0 ? DeviceType.Mobile : DeviceType.Desktop;
return deviceType ?? DeviceType.Mobile;
}else{
if(context != null){
double deviceWidth = MediaQuery.of(context).size.width;
double deviceHeight = MediaQuery.of(context).size.height;
if (deviceWidth > FormFactor.desktop || deviceWidth > deviceHeight * 1.1) {
deviceType = DeviceType.Desktop;
} else if (deviceWidth > FormFactor.handset) {
deviceType = DeviceType.Mobile;
}
return deviceType ?? DeviceType.Mobile;
}else{
return DeviceType.Mobile;
}
}
}