prefer_dedicated_media_query_functions
Severity | Quick Fix | Options |
---|---|---|
Info | ✅ | ❌ |
Details
PREFER using dedicated MediaQuery functions instead of accessing the properties from MediaQuery.of(context) or MediaQuery.maybeOf(context).
Using MediaQuery.of(context) or MediaQuery.maybeOf(context) to access below properties will cause unnecessary rebuilds.
Properties:
- accessibleNavigation
- alwaysUse24HourFormat
- boldText
- devicePixelRatio
- disableAnimations
- displayFeatures
- gestureSettings
- highContrast
- invertColors
- navigationMode
- onOffSwitchLabels
- orientation
- padding
- platformBrightness
- size
- systemGestureInsets
- textScaleFactor (deprecated after v3.12.0-2.0.pre)
- textScaler
- viewInsets
- viewPadding
Bad
final size = MediaQuery.of(context).size;
final orientation = MediaQuery.maybeOf(context)?.orientation;
final mediaQuery = MediaQuery.of(context);
final platformBrightness = mediaQuery.platformBrightness;
Good
Good
final size = MediaQuery.sizeOf(context);
final orientation = MediaQuery.maybeOrientationOf(context);
final platformBrightness = MediaQuery.platformBrightnessOf(context);