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,
})

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,
}) {
  final platform = isIOS ?? Platform.isIOS;
  final screenWidth = MediaQuery.of(context).size.width;
  final screenHeight = MediaQuery.of(context).size.height;
  final maxAdWidth = screenWidth * 0.8;
  final maxAdHeight = screenHeight * 0.8;

  double? apiWidth;
  double? apiHeight;

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

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

    if (apiHeightPath != null) {
      apiHeight = _extractValueFromPath(adData, apiHeightPath);
    } else {
      apiHeight = adData["ad"]?["elements"]?["height"]?.toDouble();
    }
  }

  // Apply fallback logic with min comparison:
  // - If user input exists: min(userInput, screen * 0.8)
  // - If only API response exists: min(apiResponse, screen * 0.8)
  // - If neither exists: screen * 0.8
  final double width;
  if (userWidth != null) {
    width = min(userWidth, maxAdWidth);
  } else if (apiWidth != null) {
    width = min(apiWidth, maxAdWidth);
  } else {
    width = maxAdWidth;
  }

  final double height;
  if (userHeight != null) {
    height = min(userHeight, maxAdHeight);
  } else if (apiHeight != null) {
    height = min(apiHeight, maxAdHeight);
  } else {
    height = maxAdHeight;
  }

  return {
    'width': width,
    'height': height,
  };
}