Created
December 8, 2020 16:27
-
-
Save samuelematias/c7fcbb36ed547fd229876efff1261569 to your computer and use it in GitHub Desktop.
[OLD] check some infos about the device in flutter.
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 '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