ResponsiveData.fromMediaQuery constructor

ResponsiveData.fromMediaQuery(
  1. MediaQueryData? media,
  2. ResponsiveConfig config
)

✅ Safe Calculation: حماية من القيم الصفرية (Web/Desktop init)

Implementation

factory ResponsiveData.fromMediaQuery(MediaQueryData? media, ResponsiveConfig config) {
  // 1. حماية: إذا كان الـ media null
  if (media == null) {
    return ResponsiveData.identity(config: config);
  }

  // 2. حماية: إذا كانت الأبعاد صفر (يحدث في الويب أحياناً قبل التحميل الكامل)
  final width = media.size.width.clamp(0.0, double.infinity).toDouble();
  final height = media.size.height.clamp(0.0, double.infinity).toDouble();

  if (width == 0 || height == 0) {
    return ResponsiveData.identity(config: config);
  }

  // المنطق الأساسي للحساب
  ScreenType type;
  if (width < config.watchBreakpoint) {
    type = ScreenType.watch;
  } else if (width < config.mobileBreakpoint) {
    type = ScreenType.mobile;
  } else if (width < config.tabletBreakpoint) {
    type = ScreenType.tablet;
  } else if (width < config.smallDesktopBreakpoint) {
    type = ScreenType.smallDesktop;
  } else if (width < config.desktopBreakpoint) {
    type = ScreenType.desktop;
  } else {
    type = ScreenType.largeDesktop;
  }

  double baseWidthScale;
  switch (type) {
    case ScreenType.watch:
      baseWidthScale = 0.67;
      break;
    case ScreenType.mobile:
      baseWidthScale = 1.0;
      break;
    case ScreenType.tablet:
      baseWidthScale = 1.22;
      break;
    case ScreenType.smallDesktop:
      baseWidthScale = 1.44;
      break;
    case ScreenType.desktop:
      baseWidthScale = 1.67;
      break;
    case ScreenType.largeDesktop:
      baseWidthScale = 1.89;
      break;
  }

  const double designHeight = 800.0;
  double rawHeightScale = (height / designHeight).clamp(0.5, 3.5).toDouble();
  final clampedWidthScale = baseWidthScale.clamp(config.minScale, config.maxScale).toDouble();

  final scaleWidth = clampedWidthScale;
  final scaleHeight = rawHeightScale.clamp(config.minScale, config.maxScale).toDouble();
  final combined = ((scaleWidth + scaleHeight) / 2).clamp(config.minScale, config.maxScale).toDouble();

  return ResponsiveData(
    size: Size(width, height),
   textScaleFactor: media.textScaler.scale(10) / 10,// media is guaranteed non-null here
    screenType: type,
    config: config,
    scaleWidth: scaleWidth,
    scaleHeight: scaleHeight,
    scaleFactor: combined,
  );
}