MediaQuery helps your app adjust its layout based on the screen size and orientation. This makes your app look good on all devices.
MediaQuery for responsiveness in Flutter
MediaQuery.of(context).size.width MediaQuery.of(context).size.height MediaQuery.of(context).orientation
Use MediaQuery.of(context) inside a widget's build method.
size gives screen width and height in logical pixels.
double screenWidth = MediaQuery.of(context).size.width;double screenHeight = MediaQuery.of(context).size.height;Orientation orientation = MediaQuery.of(context).orientation;
This app shows a blue box that changes height based on device orientation. It also displays the screen width, height, and orientation inside the box.
import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('MediaQuery Example')), body: const ResponsiveBox(), ), ); } } class ResponsiveBox extends StatelessWidget { const ResponsiveBox({super.key}); @override Widget build(BuildContext context) { double width = MediaQuery.of(context).size.width; double height = MediaQuery.of(context).size.height; Orientation orientation = MediaQuery.of(context).orientation; return Center( child: Container( width: width * 0.8, height: orientation == Orientation.portrait ? height * 0.3 : height * 0.6, color: Colors.blue, child: Center( child: Text( 'Width: ${width.toStringAsFixed(0)}\nHeight: ${height.toStringAsFixed(0)}\nOrientation: ${orientation.name}', style: const TextStyle(color: Colors.white, fontSize: 18), textAlign: TextAlign.center, ), ), ), ); } }
Always call MediaQuery.of(context) inside the build method or where context is valid.
Use orientation.name to get 'portrait' or 'landscape' as a string.
Logical pixels are device-independent pixels, so your UI scales well on different screens.
MediaQuery helps your app adapt to different screen sizes and orientations.
Use MediaQuery.of(context).size to get screen width and height.
Use MediaQuery.of(context).orientation to check if device is portrait or landscape.
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
