Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[OLD] check some infos about the device in flutter.
import 'dart:ui' as ui;
import 'dart:io';
class DeviceInfo {
static double devicePixelRatio = ui.window.devicePixelRatio;
static ui.Size size = ui.window.physicalSize;
static double width = size.width;
static double height = size.height;
static double screenWidth = width / devicePixelRatio;
static double screenHeight = height / devicePixelRatio;
static ui.Size screenSize = ui.Size(screenWidth, screenHeight);
final bool isTablet, isPhone, isIos, isAndroid, isIphoneX;
static DeviceInfo _device;
DeviceInfo({
this.isTablet,
this.isPhone,
this.isIos,
this.isAndroid,
this.isIphoneX,
});
factory DeviceInfo.get() {
if (_device != null) return _device;
bool isTablet;
bool isPhone;
final bool isIos = Platform.isIOS;
final bool isAndroid = Platform.isAndroid;
bool isIphoneX = false;
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;
}
if (isIos &&
(screenHeight == 812 ||
screenWidth == 812 ||
screenHeight == 896 ||
screenWidth == 896)) isIphoneX = true;
return _device = DeviceInfo(
isTablet: isTablet,
isPhone: isPhone,
isAndroid: isAndroid,
isIos: isIos,
isIphoneX: isIphoneX);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment