Reputation: 2700
As far as I know we can easily detect operating system in flutter. Now I was just wandering if there is any way to determine what device is the app running at. For example if app is running on mobile device or tablet device. They both can have same operating system. Reason is that due to screen size I want to display in one of my widget for example icon on mobile device and text with icon on tablet device. I know we can use MediaQuery.of(context).size.width
to find a width of the screen however I'm not sure if this is the right way of doing this and if so what are the reliable breakpoints for each device
Upvotes: 4
Views: 10702
Reputation: 899
You can use the following code snippet from flutter_device_type package:
import 'dart:ui' as ui;
bool isTablet;
bool isPhone;
final double devicePixelRatio = ui.window.devicePixelRatio;
final ui.Size size = ui.window.physicalSize;
final double width = size.width;
final double height = size.height;
if(devicePixelRatio < 2 && (width >= 1000 || height >= 1000)) {
isTablet = true;
isPhone = false;
}
else if(devicePixelRatio == 2 && (width >= 1920 || height >= 1920)) {
isTablet = true;
isPhone = false;
}
else {
isTablet = false;
isPhone = true;
}
Or for more comprehensive information you can use device_info package.
Upvotes: 7