calculateDimensions static method

Map<String, double> calculateDimensions({
  1. required BuildContext context,
  2. double? userWidth,
  3. double? userHeight,
  4. required Map<String, dynamic> adData,
  5. bool? isIOS,
  6. List<String>? apiWidthPath,
  7. List<String>? apiHeightPath,
  8. double screenPercent = 1.0,
})

Calculates width and height for ads with proper fallback logic.

context - BuildContext to get screen dimensions userWidth - User-provided width (highest priority) userHeight - User-provided height (highest priority) adData - Ad data map containing API response dimensions isIOS - Platform flag (defaults to Platform.isIOS) apiWidthPath - Custom path to extract width from adData (for special cases) apiHeightPath - Custom path to extract height from adData (for special cases)

Returns a Map with 'width' and 'height' keys containing calculated dimensions.

Implementation

static Map<String, double> calculateDimensions({
  required BuildContext context,
  double? userWidth,
  double? userHeight,
  required Map<String, dynamic> adData,
  bool? isIOS,
  List<String>? apiWidthPath,
  List<String>? apiHeightPath,
  double screenPercent = 1.0
}) {
  final platform = isIOS ?? Platform.isIOS;
  // Get full screen size
  final Size screenSize = MediaQuery.of(context).size;
  // Get safe area paddings (system insets)
  final EdgeInsets safePadding = MediaQuery.of(context).padding;

  // Calculate available safe area dimensions
  double maxAdWidth = (screenSize.width - safePadding.left - safePadding.right) * screenPercent;
  double maxAdHeight = (screenSize.height - safePadding.top - safePadding.bottom) * screenPercent;

  double? apiWidth;
  double? apiHeight;

  if (platform) {
    // iOS: Default path is adData["elements"]["width/height"]
    if (apiWidthPath == null) {
      apiWidth = double.tryParse((adData["elements"]?["width"]).toString());
    } else {
      apiWidth = _extractValueFromPath(adData, apiWidthPath);
    }

    if (apiHeightPath == null) {
      apiHeight = double.tryParse((adData["elements"]?["height"]).toString());
    } else {
      apiHeight = _extractValueFromPath(adData, apiHeightPath);
    }
  } else {
    // Android: Default path is adData["ad"]["elements"]["width/height"]
    if (apiWidthPath == null) {
      apiWidth = adData["ad"]?["elements"]?["width"]?.toDouble();
    } else {
      apiWidth = _extractValueFromPath(adData, apiWidthPath);
    }

    if (apiHeightPath == null) {
      apiHeight = adData["ad"]?["elements"]?["height"]?.toDouble();
    } else {
      apiHeight = _extractValueFromPath(adData, apiHeightPath);
    }
  }
  if (userWidth != null && userHeight != null) {
    return {
      'width': min(userWidth, maxAdWidth),
      'height': min(userHeight, maxAdHeight),
    };
  }

  if (userWidth != null) {
    final clampedWidth = min(userWidth, maxAdWidth);
    double height;
    if (apiWidth != null && apiHeight != null && apiWidth > 0) {
      height = min(clampedWidth * (apiHeight / apiWidth), maxAdHeight);
    } else {
      height = maxAdHeight;
    }
    return {'width': clampedWidth, 'height': height};
  }

  if (userHeight != null) {
    final clampedHeight = min(userHeight, maxAdHeight);
    double width;
    if (apiWidth != null && apiHeight != null && apiHeight > 0) {
      width = min(clampedHeight * (apiWidth / apiHeight), maxAdWidth);
    } else {
      width = maxAdWidth;
    }
   return {'width': width, 'height': clampedHeight};
  }

  if (apiWidth != null && apiHeight != null && apiWidth > 0 && apiHeight > 0) {
    final aspectRatio = apiHeight / apiWidth;
    double width = min(apiWidth, maxAdWidth);
    double height = width * aspectRatio;
    if (height > maxAdHeight) {
      height = maxAdHeight;
      width = height / aspectRatio;
    }
    return {'width': width, 'height': height};
  }
  return {'width': maxAdWidth, 'height': maxAdHeight};
}