Challenge - 5 Problems
Responsive Layout Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
How does this Flutter code behave on different screen widths?
Consider this Flutter widget that uses LayoutBuilder to adapt its layout. What will be the displayed text on a screen width of 500 pixels?
Flutter
LayoutBuilder(
builder: (context, constraints) {
if (constraints.maxWidth > 600) {
return Text('Wide screen');
} else {
return Text('Narrow screen');
}
},
)Attempts:
2 left
💡 Hint
Think about the condition checking maxWidth and the given screen width.
✗ Incorrect
The LayoutBuilder checks if maxWidth is greater than 600. Since 500 is less than 600, it returns the Text widget with 'Narrow screen'.
🧠 Conceptual
intermediate1:30remaining
What is the main purpose of using MediaQuery in Flutter for responsive design?
Why do Flutter developers use MediaQuery when building responsive layouts?
Attempts:
2 left
💡 Hint
Think about what information is needed to adapt UI to different devices.
✗ Incorrect
MediaQuery provides information about the device's screen size, pixel density, and orientation, which helps build layouts that adapt to different screen sizes.
📝 Syntax
advanced2:30remaining
Which Flutter widget correctly implements a responsive grid that shows 4 columns on wide screens and 2 columns on narrow screens?
Choose the correct code snippet that uses GridView and LayoutBuilder to create a responsive grid with 4 columns if width > 800, else 2 columns.
Attempts:
2 left
💡 Hint
Check the condition for crossAxisCount and how LayoutBuilder is used.
✗ Incorrect
Option A correctly uses LayoutBuilder to check maxWidth and sets crossAxisCount to 4 if width > 800, else 2. Other options either hardcode columns or reverse the condition.
❓ lifecycle
advanced2:00remaining
What happens to a StatefulWidget when device orientation changes in Flutter?
When the device orientation changes (e.g., portrait to landscape), what is the typical behavior of a StatefulWidget in Flutter?
Attempts:
2 left
💡 Hint
Think about how Flutter handles widget rebuilds and state preservation.
✗ Incorrect
Flutter rebuilds widgets on orientation change but preserves the State object, so stateful data is kept unless explicitly reset.
🔧 Debug
expert3:00remaining
Why does this responsive Flutter layout fail to update when rotating the device?
This code uses MediaQuery.of(context).size.width in initState to set a variable for layout. Why does the layout not update on device rotation?
Flutter
class MyWidget extends StatefulWidget { @override State<MyWidget> createState() => _MyWidgetState(); } class _MyWidgetState extends State<MyWidget> { late double screenWidth; @override void initState() { super.initState(); // Moved MediaQuery call to didChangeDependencies } @override void didChangeDependencies() { super.didChangeDependencies(); screenWidth = MediaQuery.of(context).size.width; } @override Widget build(BuildContext context) { return Text(screenWidth > 600 ? 'Wide' : 'Narrow'); } }
Attempts:
2 left
💡 Hint
Consider when MediaQuery data is available and how state variables update.
✗ Incorrect
MediaQuery.of(context) should not be called in initState because context is not fully available. Also, screenWidth is set once and not updated on rebuild, so the UI does not respond to orientation changes. The build method uses the stored variable instead of querying MediaQuery again.