What does MediaQuery.of(context).size return in a Flutter app?
Think about what information you need to make your UI adapt to different devices.
MediaQuery.of(context).size returns the width and height of the device screen in logical pixels, which helps you build responsive layouts.
Given this Flutter code snippet, what will be the width of the Container on a device with screen width 400 logical pixels?
Container( width: MediaQuery.of(context).size.width * 0.5, height: 100, color: Colors.blue, )
Multiply the screen width by 0.5 to find the container width.
The container width is half the screen width, so 400 * 0.5 = 200 logical pixels.
In Flutter, when does MediaQuery.of(context) update its values during app usage?
Think about what events affect screen size or layout.
MediaQuery updates when the device orientation or screen size changes, allowing responsive layouts to adapt dynamically.
Consider this Flutter widget tree snippet:
Widget build(BuildContext context) {
return Container(
width: MediaQuery.of(context).size.width,
height: 100,
);
}Why might MediaQuery.of(context) throw an error here?
Think about what widgets provide MediaQuery data.
MediaQuery.of(context) requires a MediaQuery widget above in the widget tree, usually provided by MaterialApp or WidgetsApp. Without it, it throws an error.
You want your Flutter app to show a different layout on phones and tablets. Which approach using MediaQuery is best?
Think about a property that helps distinguish phone vs tablet screen size.
Using shortestSide helps detect if the device is a tablet or phone by checking if the smaller screen dimension passes a threshold (e.g., 600 logical pixels).