What if your app could magically fit every screen perfectly without extra work?
Why MediaQuery for responsiveness in Flutter? - Purpose & Use Cases
Imagine you build a mobile app that looks great on your phone. But when you open it on a tablet or a bigger screen, everything looks too small or too big, and buttons are misplaced.
You try to guess screen sizes and set fixed widths and heights for each device manually.
This manual approach is slow and frustrating because there are so many device sizes and orientations.
You end up writing lots of code for each screen size, which is hard to maintain and easy to break.
MediaQuery in Flutter helps you get the current screen size and orientation automatically.
It lets your app adjust layouts and sizes dynamically, so your UI looks good on any device without guessing.
Container(width: 300, height: 600) // fixed size, may not fit all screens
Container(width: MediaQuery.of(context).size.width * 0.8, height: MediaQuery.of(context).size.height * 0.5) // responsive size
With MediaQuery, your app can adapt smoothly to all screen sizes and orientations, giving users a great experience everywhere.
Think of a photo gallery app that shows a grid of images. Using MediaQuery, the app can show more columns on a tablet and fewer on a phone, making the best use of space.
Manual fixed sizes don't work well for many devices.
MediaQuery provides real-time screen info to build flexible layouts.
This leads to apps that look good and work well on any screen.
Practice
MediaQuery.of(context).size provide in a Flutter app?Solution
Step 1: Understand MediaQuery size property
MediaQuery.of(context).sizereturns the screen's width and height as a Size object.Step 2: Differentiate from other properties
Theme colors, fonts, and app state are unrelated to MediaQuery size.Final Answer:
The width and height of the device screen -> Option CQuick Check:
MediaQuery size = screen dimensions [OK]
- Confusing size with theme or app state
- Thinking it returns orientation
- Assuming it gives device model info
Solution
Step 1: Recall correct MediaQuery syntax
The correct way isMediaQuery.of(context).orientationto access orientation.Step 2: Eliminate incorrect options
Other options use invalid method names or wrong property access.Final Answer:
MediaQuery.of(context).orientation -> Option BQuick Check:
Use .of(context) to access MediaQuery properties [OK]
- Omitting .of(context)
- Using wrong method names
- Accessing properties directly on MediaQuery
final size = MediaQuery.of(context).size;
final width = size.width;
final height = size.height;
print('Width: $width, Height: $height');
What will this print if the device screen is 360 pixels wide and 640 pixels tall?Solution
Step 1: Understand MediaQuery size values
MediaQuery.of(context).size.widthgives screen width (360), height gives screen height (640).Step 2: Match printed output
The print statement outputs width first then height, so it prints "Width: 360, Height: 640".Final Answer:
Width: 360, Height: 640 -> Option AQuick Check:
Width = 360, Height = 640 [OK]
- Swapping width and height values
- Assuming zero or null values
- Confusing print output format
double width = MediaQuery.size.width;What is the main problem?
Solution
Step 1: Check MediaQuery usage
MediaQuery properties require calling.of(context)to access size.Step 2: Identify missing .of(context)
The code misses.of(context), soMediaQuery.sizeis invalid.Final Answer:
MediaQuery.size is not a valid property; .of(context) is missing -> Option DQuick Check:
Always use MediaQuery.of(context) [OK]
- Forgetting .of(context)
- Confusing property order
- Wrong data type assumptions
Solution
Step 1: Use correct orientation check
MediaQuery.of(context).orientation returns an enum Orientation.portrait or Orientation.landscape.Step 2: Verify correct comparison and widget return
if (MediaQuery.of(context).orientation == Orientation.portrait) { return PortraitWidget(); } else { return LandscapeWidget(); } compares orientation to Orientation.portrait correctly and returns widgets accordingly.Step 3: Eliminate wrong options
if (MediaQuery.orientation(context) == 'portrait') { return PortraitWidget(); } else { return LandscapeWidget(); } uses wrong syntax, C compares sizes incorrectly, D reverses widget logic.Final Answer:
if (MediaQuery.of(context).orientation == Orientation.portrait) { return PortraitWidget(); } else { return LandscapeWidget(); } -> Option AQuick Check:
Compare orientation enum, not strings or sizes [OK]
- Comparing orientation to strings
- Using size to guess orientation
- Reversing widget logic
Ready to prove it?
Pass the test and this pattern moves unknown
