0
0
Fluttermobile~20 mins

MediaQuery for responsiveness in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MediaQuery Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
1:30remaining
How does MediaQuery help in Flutter layouts?

What does MediaQuery.of(context).size return in a Flutter app?

AThe size of the device screen in logical pixels
BThe size of the widget tree currently built
CThe size of the app bar only
DThe size of the keyboard on screen
Attempts:
2 left
💡 Hint

Think about what information you need to make your UI adapt to different devices.

ui_behavior
intermediate
1:30remaining
Using MediaQuery to set widget width

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,
)
A200 logical pixels
B100 logical pixels
C400 logical pixels
D50 logical pixels
Attempts:
2 left
💡 Hint

Multiply the screen width by 0.5 to find the container width.

lifecycle
advanced
2:00remaining
When does MediaQuery update its values?

In Flutter, when does MediaQuery.of(context) update its values during app usage?

AEvery time a widget rebuilds regardless of screen changes
BWhen the device orientation or screen size changes
COnly once when the app starts
DOnly when the app is restarted
Attempts:
2 left
💡 Hint

Think about what events affect screen size or layout.

🔧 Debug
advanced
2:00remaining
Why does MediaQuery.of(context) throw an error?

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?

ABecause MediaQuery only works inside StatefulWidgets
BBecause Container cannot have width set from MediaQuery
CBecause MediaQuery requires async context
DBecause there is no MediaQuery ancestor in the widget tree at this context
Attempts:
2 left
💡 Hint

Think about what widgets provide MediaQuery data.

🧠 Conceptual
expert
2:30remaining
How to handle different screen sizes with MediaQuery?

You want your Flutter app to show a different layout on phones and tablets. Which approach using MediaQuery is best?

AUse <code>MediaQuery.of(context).orientation</code> only to decide layout
BHardcode pixel values for each device type without MediaQuery
CCheck <code>MediaQuery.of(context).size.shortestSide</code> and use a threshold to decide layout
DUse <code>MediaQuery.of(context).padding</code> to decide layout
Attempts:
2 left
💡 Hint

Think about a property that helps distinguish phone vs tablet screen size.