Device.get constructor

Device.get()

Implementation

factory Device.get() {
  if (_device != null) return _device!;

  if (onMetricsChange == null) {
    onMetricsChange = ui.window.onMetricsChanged;
    ui.window.onMetricsChanged = () {
      _device = null;

      size = ui.window.physicalSize;
      width = size.width;
      height = size.height;
      screenWidth = width / devicePixelRatio;
      screenHeight = height / devicePixelRatio;
      screenSize = ui.Size(screenWidth, screenHeight);

      onMetricsChange!();
    };
  }

  bool isTablet;
  bool isPhone;
  bool isIos = Platform.isIOS;
  bool isAndroid = Platform.isAndroid;
  bool isIphoneX = false;
  bool hasNotch = 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;
  }

  // Recalculate for Android Tablet using device inches
  if (isAndroid) {
    final adjustedWidth = _calWidth() / devicePixelRatio;
    final adjustedHeight = _calHeight() / devicePixelRatio;
    final diagonalSizeInches = (math.sqrt(
        math.pow(adjustedWidth, 2) + math.pow(adjustedHeight, 2))) /
        _ppi;
    //print("Dialog size inches is $diagonalSizeInches");
    if (diagonalSizeInches >= 7) {
      isTablet = true;
      isPhone = false;
    } else {
      isTablet = false;
      isPhone = true;
    }
  }

  if (isIos &&
      isPhone &&
      (screenHeight == 812 ||
          screenWidth == 812 ||
          screenHeight == 896 ||
          screenWidth == 896 ||
          // iPhone 12 pro
          screenHeight == 844 ||
          screenWidth == 844 ||
          // Iphone 12 pro max
          screenHeight == 926 ||
          screenWidth == 926)) {
    isIphoneX = true;
    hasNotch = true;
  }

  if (_hasTopOrBottomPadding()) hasNotch = true;

  return _device = Device(
      isTablet: isTablet,
      isPhone: isPhone,
      isAndroid: isAndroid,
      isIos: isIos,
      isIphoneX: isIphoneX,
      hasNotch: hasNotch);
}