Sub topic 4
Fixing Layout Problems and Overflow Errors
This chapter assumes you know basic Dart, can build simple StatelessWidget and StatefulWidget classes, and can run a Flutter app on an emulator, simulator, or device. You should also understand that a Flutter screen is a tree of widgets built by the build method.
Layout bugs often appear as yellow-and-black stripes, clipped content, unexpected spacing, or widgets that disappear. Flutter DevTools helps you inspect the widget tree and see how constraints produce those results.
The Flutter constraint model
Flutter layout follows a simple rule:
- A parent gives its child constraints.
- The child chooses a size within those constraints.
- The parent positions the child.
A constraint contains minimum and maximum widths and heights. For example, a screen might give a Column a maximum width of 390 logical pixels. The Column then passes constraints to its children.
A layout error usually means that a widget received constraints different from what you expected. Common examples include:
| Symptom | Likely cause | Useful tool |
|---|---|---|
| Yellow-and-black overflow stripe | A child is larger than its parent | Layout Explorer |
| Text disappears or is clipped | A fixed size is too small | Widget Inspector |
| Large empty area | Incorrect alignment or loose constraints | Widget Inspector |
| Widget has zero size | An ancestor gave no usable constraint | Layout Explorer |
A Row lays children out horizontally. Its non-flex children can receive an unbounded width, which means they may try to use as much horizontal space as they need. A long Text widget can then exceed the screen width.
Inspecting the widget tree
Start your app and connect DevTools. Open the Inspector tab, then select the Widget Inspector. You can select a widget in the running app or select it from the tree.
The Inspector shows:
- The widget's location in the tree.
- Its size and position.
- Parent and child relationships.
- Padding, alignment, and other properties.
- Paint and layout boundaries.
Turn on the Debug Paint option when you need to see widget bounds, padding, baselines, and alignment. Use the Select Widget Mode button to identify the widget under the pointer.
When Flutter reports a RenderFlex overflow, select the affected Row, Column, or other flex widget. The Inspector can open the Layout Explorer, which displays the flex direction, available space, child sizes, and flex factors.
Fixing an overflow with constraints
The following example puts an icon and long text in a Row:
import 'package:flutter/material.dart';
class OverflowDemo extends StatelessWidget {
const OverflowDemo({super.key});
@override
Widget build(BuildContext context) {
return const Row(
children: [
Icon(Icons.info),
// Without Expanded, this long text overflows the Row.
Expanded(
child: Text(
'A very long message that does not fit on one line of a narrow screen.',
),
),
],
);
}
}Expanded is a flex widget. It tells the Row to give the text the remaining available width. The Text widget can then wrap its content instead of requesting more width than the row has.
Other fixes depend on the intended design:
- Use
Flexiblewhen the child may use less than all remaining space. - Set
maxLinesandoverflow: TextOverflow.ellipsisfor a single-line label. - Use
Wrapwhen items should move to another line. - Make a tall page scrollable with
SingleChildScrollVieworListView. - Avoid fixed widths when the screen must support different device sizes.
- Check both portrait and landscape orientations.
Do not hide an overflow by simply clipping it. Clipping can conceal a real usability problem, especially when it removes text or controls.
A repeatable inspection process
Use this sequence when a layout looks wrong:
- Reproduce the problem on the smallest realistic screen.
- Open the Inspector and select the visible or affected widget.
- Follow its ancestors upward until you find the widget imposing the unexpected constraint.
- Open Layout Explorer for a
Row,Column, orFlex. - Check whether the child needs
Expanded,Flexible,Wrap, or scrolling. - Apply one focused change.
- Hot reload and reproduce the same case.
- Test a second screen size and confirm that the overflow message is gone.
Exercise
A Row contains an icon and a long Text widget. The text overflows on narrow screens. What should you inspect first, and what is a common fix?
Show answer
Select the Row in the Widget Inspector and inspect its constraints in Layout Explorer. A common fix is to wrap the text in Expanded or Flexible so it receives a bounded width and can wrap.