Sub topic 15
Inspecting layouts with Flutter DevTools
Assume you know basic Dart, StatelessWidget, and build, and that you can run a Flutter app. DevTools helps you inspect the layout that Flutter actually produced, rather than guessing from the widget code.
Open the Inspector
Run the app in debug mode, then open Flutter DevTools. Select the Inspector tab. The inspector shows the widget tree for the current screen.
Use Select Widget Mode to click a visible widget in the running app. DevTools selects the corresponding node in the tree and shows details such as:
- The widget's parent and children.
- Its render object and layout properties.
- Its size and position.
- The constraints passed to it.
- Flex information when it belongs to a
RoworColumn.
The widget tree is the starting point for layout debugging. Select the smallest widget that looks wrong, then move upward through its parents. A child may have the correct code but receive unsuitable constraints from its parent.
Read constraints and sizes
Flutter lays out a widget in two directions:
- A parent sends constraints down.
- The child chooses a size within those constraints.
- The child reports its size up.
- The parent positions the child.
DevTools makes this process visible. For example, a Row might receive a width of 360 logical pixels. If its children need 400 pixels and none can shrink, the row reports a RenderFlex overflowed error.
| Symptom | Inspect first | Common fix |
|---|---|---|
| Yellow and black overflow stripe | The overflowing `Row` or `Column` | Use `Expanded`, `Flexible`, `Wrap`, or scrolling |
| “Unbounded height” error | A scrollable inside a `Column` | Give it a bounded height with `Expanded` |
| Unexpected empty space | `mainAxisSize` and alignment | Check `MainAxisSize` and `MainAxisAlignment` |
| Child appears outside its container | `Stack` constraints and clipping | Check `Positioned` values and stack size |
The Layout Explorer is especially useful for flex layouts. It displays the main axis, cross axis, available space, flex factors, and alignment choices. Select each child to see whether Expanded, Flexible, or a fixed-size widget is consuming the space you expected.
Reproduce and isolate the problem
Keep a deliberately small reproduction while debugging. This app gives the text a bounded width, so it can shrink instead of overflowing:
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: DebugLayoutPage()));
}
class DebugLayoutPage extends StatelessWidget {
const DebugLayoutPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Layout inspection')),
body: Padding(
padding: const EdgeInsets.all(16),
child: Row(
children: [
const Icon(Icons.info),
const SizedBox(width: 8),
Expanded(
child: Text(
'This message can become very long on a narrow screen.',
overflow: TextOverflow.ellipsis,
),
),
const SizedBox(width: 8),
const Icon(Icons.chevron_right),
],
),
),
);
}
}Toggle the problematic widget in the inspector and compare its bounds with its parent. If the text is not inside Expanded, the row may have no way to negotiate a smaller width.
Use visual debugging tools
DevTools can highlight widget bounds and show repaint areas. These overlays help distinguish a sizing problem from a painting problem. For a flex error, start with constraints and flex factors. For a stack error, inspect the stack's size, the child order, and each Positioned edge.
Exercise
A ListView inside a Column reports an unbounded-height error. What should you inspect and what common fix should you try?
Show answer
Inspect the Column and the ListView in DevTools. The Column gives the list unbounded height, so wrap the ListView in Expanded or otherwise provide a finite height.