AdaptiveDeviceInfo.of constructor

AdaptiveDeviceInfo.of(
  1. BuildContext context, {
  2. required AdaptiveBreakpoints breakpoints,
  3. required DesignSize designSizeConfig,
  4. required ScreenSizeBreakpoints screenSizeBreakpoints,
})

Creates device info from a BuildContext.

Implementation

factory AdaptiveDeviceInfo.of(
  BuildContext context, {
  required AdaptiveBreakpoints breakpoints,
  required DesignSize designSizeConfig,
  required ScreenSizeBreakpoints screenSizeBreakpoints,
}) {
  final mediaQuery = MediaQuery.of(context);
  final size = mediaQuery.size;
  final width = size.width;
  final height = size.height;
  final orientation = mediaQuery.orientation;
  final deviceType = breakpoints.getDeviceType(width, height);
  final screenSize = screenSizeBreakpoints.getScreenSize(width);

  // Select appropriate design size based on device type
  Size designSize;
  switch (deviceType) {
    case DeviceType.phone:
    case DeviceType.foldable:
      designSize = designSizeConfig.phone;
      break;
    case DeviceType.tablet:
      designSize = designSizeConfig.tablet;
      break;
    case DeviceType.desktop:
      designSize = designSizeConfig.desktop;
      break;
  }

  return AdaptiveDeviceInfo(
    screenWidth: width,
    screenHeight: height,
    orientation: orientation,
    pixelRatio: mediaQuery.devicePixelRatio,
    deviceType: deviceType,
    screenSize: screenSize,
    designSize: designSize,
    statusBarHeight: mediaQuery.padding.top,
    bottomBarHeight: mediaQuery.padding.bottom,
    textScaleFactor: mediaQuery.textScaler.scale(1.0),
    safeAreaPadding: mediaQuery.padding,
  );
}