Sub topic 6
Defining breakpoints with window-size classes
Assume you know basic Dart, widget trees, Row, Column, Scaffold, and ListView. This chapter focuses on choosing a layout from the width available to a widget. It does not treat phone, tablet, and desktop as separate device types.
A breakpoint is a width at which your interface changes structure. For example, a narrow window might show one content pane, while a wider window might show a list beside its details. Good breakpoints describe the space your layout needs, not the brand or model of a device.
Use window-size classes
Material 3 defines three useful window-size classes:
| Class | Width | Typical layout |
|---|---|---|
| Compact | Less than 600 logical pixels | One column or bottom navigation |
| Medium | 600 to less than 840 logical pixels | More breathing room or a narrow two-pane layout |
| Expanded | 840 logical pixels or more | Two panes, navigation rail, or wider grid |
A logical pixel is Flutter's layout unit. It is different from a physical screen pixel because Flutter accounts for device pixel density.
Keep the classification in one helper. This prevents slightly different thresholds from appearing throughout the widget tree.
enum WindowSize { compact, medium, expanded }
WindowSize windowSizeFor(double width) {
if (width < 600) return WindowSize.compact;
if (width < 840) return WindowSize.medium;
return WindowSize.expanded;
}The comparison uses <, so a width of exactly 600 is medium, and a width of exactly 840 is expanded. State these boundary rules in tests and documentation.
Choose the width source
Use MediaQuery.sizeOf(context) when the decision concerns the app window or screen. It works well for a top-level shell that chooses between a NavigationBar and a NavigationRail.
Use LayoutBuilder when the decision concerns the space given to one widget. A panel might be narrow because it sits beside a permanent sidebar, even though the whole window is wide.
This distinction prevents a common error: using the whole screen width to select a layout that does not actually fit inside its parent.
Apply classes at a layout boundary
Classify the width near the widget that changes shape, then switch on the result. The boundary should describe a meaningful change, such as adding a second pane or changing navigation.
import 'package:flutter/material.dart';
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')),
};
},
);
}
}The layout still needs flexible children. A breakpoint should choose a structure, not replace Expanded, scrolling, or text wrapping. A medium layout can fail if its two children have fixed widths that exceed the available space.
Exercise
Exercise
Which tool should you use when a card must change layout based on the width its parent gives it, even if the whole window is wide?
Show answer
Use LayoutBuilder, because its constraints describe the card's actual available space.