0
0
Fluttermobile~20 mins

Responsive layout patterns in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Responsive Layout Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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');
    }
  },
)
ADisplays 'Wide screen'
BDisplays nothing
CDisplays 'Narrow screen'
DThrows a runtime error
Attempts:
2 left
💡 Hint
Think about the condition checking maxWidth and the given screen width.
🧠 Conceptual
intermediate
1:30remaining
What is the main purpose of using MediaQuery in Flutter for responsive design?
Why do Flutter developers use MediaQuery when building responsive layouts?
ATo manage app state across widgets
BTo create animations
CTo handle user input events
DTo get device screen size and orientation information
Attempts:
2 left
💡 Hint
Think about what information is needed to adapt UI to different devices.
📝 Syntax
advanced
2: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.
A
LayoutBuilder(builder: (context, constraints) {
  int count = constraints.maxWidth > 800 ? 4 : 2;
  return GridView.count(
    crossAxisCount: count,
    children: List.generate(8, (index) => Container(color: Colors.blue)),
  );
})
B
GridView.count(
  crossAxisCount: 4,
  children: List.generate(8, (index) => Container(color: Colors.blue)),
)
C
LayoutBuilder(builder: (context, constraints) {
  int count = constraints.maxWidth > 800 ? 2 : 4;
  return GridView.count(
    crossAxisCount: count,
    children: List.generate(8, (index) => Container(color: Colors.blue)),
  );
})
D
GridView.builder(
  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3),
  itemCount: 8,
  itemBuilder: (context, index) => Container(color: Colors.blue),
)
Attempts:
2 left
💡 Hint
Check the condition for crossAxisCount and how LayoutBuilder is used.
lifecycle
advanced
2: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?
ANothing changes; orientation changes do not affect widgets
BThe widget is rebuilt but its State object is preserved
CThe widget and its State object are both destroyed and recreated
DThe widget rebuilds but loses its State object causing reset
Attempts:
2 left
💡 Hint
Think about how Flutter handles widget rebuilds and state preservation.
🔧 Debug
expert
3: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');
  }
}
AAll of the above
BThe variable screenWidth is not updated on rebuild, so it keeps old value
CThe build method does not use MediaQuery directly, so it won't react to changes
DMediaQuery.of(context) cannot be used in initState because context is not fully initialized
Attempts:
2 left
💡 Hint
Consider when MediaQuery data is available and how state variables update.