What is MediaQuery in Flutter?
MediaQuery is a powerful widget that provides information about the current device and app settings. It gives you access to the screen's dimensions, orientation, pixel density, text scaling factor, keyboard insets, and much more. MediaQuery is essential for creating responsive layouts that adapt to different screen sizes and device capabilities.
Accessing MediaQuery Data
To get the current MediaQuery data, use MediaQuery.of(context). This returns a MediaQueryData object containing all the information about the device. You can then read properties like size, orientation, devicePixelRatio, etc.
Key Properties of MediaQueryData
- size – The dimensions of the screen (in logical pixels).
- width – Shortcut for
size.width. - height – Shortcut for
size.height. - orientation – Whether the device is in portrait or landscape (
Orientation.portrait/Orientation.landscape). - devicePixelRatio – The number of device pixels for each logical pixel (used for scaling images).
- textScaleFactor – The user's preferred font size scaling (e.g., for accessibility).
- padding – The insets that parts of the display might be obscured by system UI (like the status bar or notches).
- viewPadding – Similar to padding, but includes areas that are physically present (e.g., rounded corners).
- viewInsets – The parts of the display that are completely obscured (like the keyboard).
- alwaysUse24HourFormat – Whether the user prefers 24‑hour time format.
- platformBrightness – The current brightness mode (light/dark theme).
Building Responsive Layouts
MediaQuery is the foundation of responsive design in Flutter. You can use its properties to change the layout based on screen size or orientation. For example, you might show a different number of columns in a grid depending on the screen width.
Performance Considerations: MediaQuery.of vs. Specific Getters
Calling MediaQuery.of(context) rebuilds the widget whenever any property of MediaQuery changes (e.g., orientation, text scale factor, or even the keyboard appearance). This can cause unnecessary rebuilds. To optimise performance, Flutter 3.10 introduced specific getters like MediaQuery.sizeOf(context) and MediaQuery.orientationOf(context). These only trigger rebuilds when the specific property changes.
Modifying MediaQuery
Sometimes you need to modify the MediaQuery for a subtree. For instance, you might want to remove system padding from a widget or force a different text scale factor. You can use MediaQuery.removePadding, MediaQuery.removeViewInsets, or wrap a widget with a new MediaQuery widget and provide your own MediaQueryData.
Handling Orientation Changes
You can listen to orientation changes using OrientationBuilder, which internally uses MediaQuery. It rebuilds only when the orientation changes, making it efficient.
Common Mistakes Beginners Make
- Calling MediaQuery.of(context) before the widget is inserted in the tree: This happens if you call it in a constructor or before
build. Always call it insidebuildor in a place where context is valid. - Assuming MediaQuery is always available: If you haven't placed a
MaterialApporWidgetsAppabove, there will be no MediaQuery. Make sure your app is wrapped accordingly. - Not handling null context: In some edge cases,
MediaQuery.of(context)might throw if no ancestor provides it. UsemaybeOfif you're unsure. - Overusing MediaQuery.of() and causing rebuilds: Use specific getters (
sizeOf,orientationOf) where possible. - Forgetting that padding and viewInsets include system UI: When placing content at the bottom, remember to account for the keyboard using
viewInsets.bottom.
Key Points to Remember
- MediaQuery provides essential information about the device and app settings.
- Use
MediaQuery.of(context)to get the data; use specific getters (sizeOf,orientationOf) for better performance. - Responsive layouts often depend on
size.widthororientation. - You can modify MediaQuery for a subtree using
MediaQuerywidget or helper methods likeremovePadding. - Always consider the impact of keyboard and system UI insets on your layout.
Common Interview Questions
- What is the difference between
MediaQuery.of(context).sizeandMediaQuery.sizeOf(context)? - How would you create a layout that changes from a list to a grid when the device is rotated?
- Explain the difference between
padding,viewPadding, andviewInsetsin MediaQueryData. - How can you disable the user's system‑level text scaling inside a specific part of your app?
- What happens if you try to access MediaQuery before a MaterialApp? How would you handle that?
- Describe a scenario where you would use
MediaQuery.removePadding.