Sub topic 14
Common Pitfalls and How to Avoid Them
Assume you know basic Dart, widget trees, Row, Column, Scaffold, and ListView. You should also understand Flutter's constraints model: parents send constraints down, children choose sizes, and sizes travel back up.
Responsive layouts change arrangement as space changes. Adaptive layouts choose a suitable interaction pattern, such as a NavigationBar on a phone and a NavigationRail on a wide window. Most layout bugs happen when code responds to the wrong source of information or assumes that every window behaves like one tested device.
Use the right size information
Use MediaQuery.sizeOf(context) when a decision depends on the window or screen, such as choosing a navigation pattern. Use LayoutBuilder when a decision depends on the space available to one widget.
A reusable widget might be placed inside a narrow panel even when the whole window is wide. MediaQuery still reports the wide window, but LayoutBuilder reports the panel's local constraints.
import 'package:flutter/material.dart';
class ResponsivePanel extends StatelessWidget {
const ResponsivePanel({super.key});
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) {
final twoColumns = constraints.maxWidth >= 600;
return GridView.count(
crossAxisCount: twoColumns ? 2 : 1,
children: const [
Card(child: Center(child: Text('First'))),
Card(child: Center(child: Text('Second'))),
],
);
},
);
}
}Avoid hard-coded widths and accidental overflow
A fixed width can work on one phone and overflow on another. Prefer Expanded, Flexible, Wrap, and maximum constraints. In a Row, long text should usually be inside Expanded or Flexible.
Common warning signs include:
- A yellow-and-black overflow stripe.
- A
RenderFlex overflowedmessage. - Content that disappears when text becomes larger.
- A grid tile that becomes too narrow for its label.
Do not solve every overflow by wrapping the whole page in a horizontal SingleChildScrollView. That may hide the problem and make touch interaction difficult. First identify which child needs to flex, wrap, or scroll.
Treat breakpoints as layout decisions
Breakpoints are thresholds where the interface changes structure. They should describe content needs, not imitate device names. A tablet can be used in a narrow window, and a desktop window can be resized to phone width.
| Problem | Better approach |
|---|---|
| Navigation no longer fits at the bottom | Switch to a rail |
| Cards become too narrow | Reduce columns or use a maximum tile width |
| List and detail views need simultaneous space | Use a two-pane layout |
| Text or controls overflow | Let children flex, wrap, or scroll |
Keep breakpoint rules in one place. Scattered checks such as width > 500 and width >= 600 are hard to reason about and can produce inconsistent layouts.
Respect text, insets, and input methods
Users can increase text size. Avoid fixed-height containers around text, and test labels at large text-scale settings. Use SafeArea when content could be hidden by system bars or display cutouts.
Mouse and keyboard users need hover feedback, focus traversal, and usable pointer targets. Touch users need controls large enough to tap. A layout that only works after a mouse click is incomplete.
Test the boundaries
Test just below and just above every breakpoint. Also test landscape orientation, large text, long translations, and a narrow nested panel. Widget tests should reset the simulated view after changing its size.
Exercise
A card grid is inside a side panel. The window is 1200 logical pixels wide, but the panel is 420 pixels wide. Which tool should choose the grid's column count, and why?
Show answer
Use LayoutBuilder. It receives the panel's actual constraints, while MediaQuery reports the whole window.