Sub topic 5
Reacting to Available Space with LayoutBuilder
Prerequisites
This chapter assumes that you can write basic Dart, build widget trees, and use Row, Column, Scaffold, and ListView. You should also understand that a widget receives constraints from its parent.
Why use LayoutBuilder?
LayoutBuilder is a widget that builds its child from the constraints available at that location in the widget tree. It helps a component react to its own space instead of making assumptions about the whole screen.
This distinction matters when a widget appears inside a side panel, dialog, split view, or reusable card. MediaQuery.sizeOf(context) describes the application window. LayoutBuilder describes the space offered by the widget's immediate parent.
Use MediaQuery.sizeOf for decisions about the window or device. Use LayoutBuilder for decisions about a local layout.
The constraint flow
Flutter layout follows a simple direction:
- A parent sends constraints down to its child.
- The child chooses a size within those constraints.
- The child reports its size back up to the parent.
- The parent positions the child.
LayoutBuilder lets you inspect the constraints during the build step. Its builder function receives a BoxConstraints object. The object includes values such as maxWidth and maxHeight.
Choose a layout from local width
A breakpoint is a width where the layout changes. Keeping breakpoint logic in one helper makes the decision easy to test and reuse.
import 'package:flutter/material.dart';
enum WindowSize { compact, medium, expanded }
WindowSize windowSizeFor(double width) {
if (width < 600) return WindowSize.compact;
if (width < 840) return WindowSize.medium;
return WindowSize.expanded;
}
class AdaptiveBody extends StatelessWidget {
const AdaptiveBody({super.key});
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) {
final size = windowSizeFor(constraints.maxWidth);
return switch (size) {
WindowSize.compact => const Center(child: Text('One pane')),
WindowSize.medium => const Center(child: Text('Two panes, narrow')),
WindowSize.expanded => const Center(child: Text('Two panes, wide')),
};
},
);
}
}
void main() {
runApp(
const MaterialApp(
home: Scaffold(body: AdaptiveBody()),
),
);
}The values 600 and 840 are common Material 3 window-size boundaries. They are a starting point, not a rule for every component. A toolbar might need a different threshold from a list-detail view.
Common errors
Do not use LayoutBuilder to read the entire window when the component only needs its local width. A parent may give the component less space than the window provides.
Avoid assuming that maxWidth is always finite. A horizontally scrolling parent can provide an unbounded width. In that case, compare constraints.hasBoundedWidth before using a width-based layout decision.
Also avoid putting expensive work inside the builder. The builder can run again when the constraints change. Build the small part that depends on those constraints, and keep unrelated state outside it.
Exercise
A card is placed in a narrow side panel on a wide desktop window. Should the card use MediaQuery.sizeOf or LayoutBuilder to choose between one and two columns?
Show answer
Use LayoutBuilder because the card must react to the width supplied by its parent, not the width of the whole window.