getScalingFactor static method

double getScalingFactor(
  1. BuildContext context
)

Returns a scaling factor relative to baseline width

This method calculates a scaling factor based on the current device's screen width compared to a reference width. The scaling factor is used to adjust font sizes, spacing, and other UI elements to maintain consistent visual hierarchy across different device sizes.

The reference width is set to 440.0 logical pixels (similar to iPhone 16 Pro Max), which serves as the baseline for scaling calculations. The scaling factor is clamped between 0.9 and 1.1 to prevent extreme scaling that could affect readability.

Parameters:

  • context: BuildContext for accessing MediaQuery

Returns a double scaling factor between 0.9 and 1.1

Implementation

static double getScalingFactor(BuildContext context) {
  // Get MediaQuery or return default reference width if not available
  final mediaQuery = MediaQuery.maybeOf(context);
  if (mediaQuery == null) return 1.0;

  // Check if the device is a tablet or iPad
  // A common threshold for tablets is a shortest side of 600 logical pixels
  final shortestSide = mediaQuery.size.shortestSide;
  if (shortestSide >= 600) {
    return 1.5;
  }

  final screenWidth = mediaQuery.size.width;

  // Reference width (can adjust for different baselines)
  // This represents the baseline device width for scaling calculations
  final referenceWidth = DeviceRegistry.referenceWidth; // like iPhone 16 Pro Max

  // Calculate raw scaling factor
  double scale = screenWidth / referenceWidth;

  // Limit max scale for readability
  // Prevents extreme scaling that could make text too small or too large
  return scale.clamp(0.9, 1.1);
}