Sub topic 6
Defining breakpoints with window-size classes
This chapter assumes you can write Dart 3 switch expressions, build widget trees with StatelessWidget and StatefulWidget, and use Row, Column, Scaffold and ListView. It also assumes you have read about MediaQuery and LayoutBuilder. Here you will turn raw widths into a small set of named layout categories and use them to switch layouts.
What a breakpoint is
A breakpoint is a width at which your layout changes shape. Below it you show one arrangement. At or above it you show another. Widths are measured in logical pixels, the density-independent unit Flutter uses for layout. A phone in portrait is about 360 to 430 logical pixels wide. A tablet or a desktop window can be several times wider.
Scattering numbers such as if (width > 700) across many widgets causes trouble. Two screens drift apart, and a change to the rule means a search through the whole code base. A better approach is to define the breakpoints once, give each range a name, and let every widget ask for the name.
Window-size classes
Material 3 groups widths into window-size classes. This chapter uses the three that cover most apps:
| Class | Width in logical pixels | Typical device | Typical layout |
|---|---|---|---|
| Compact | less than 600 | Phone in portrait | One pane, bottom navigation bar |
| Medium | 600 to less than 840 | Small tablet, large phone in landscape | One or two panes, navigation rail |
| Expanded | 840 or more | Large tablet, desktop, web | Two panes, navigation rail or drawer |
The ranges are half-open. 600 belongs to medium, and 840 belongs to expanded. Material guidance also describes larger classes for very wide windows. You can add them the same way if your app needs them.
The classes describe the window your app has, not the device. A desktop window dragged narrow is compact. A phone in split-screen mode can be compact even when the phone is large.
One helper for the whole app
Put the mapping in one function. Everything else calls it.
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 function takes a double, not a BuildContext. That has two benefits. You can unit test it without building any widgets. You can also feed it a width from either MediaQuery or LayoutBuilder, which matters in the next section.
Choosing the width source
The helper needs a width, and you have two places to get it.
MediaQuery.sizeOf(context).widthis the width of the whole window (more precisely, of the nearestMediaQueryabove the widget). Use it for decisions about the app's overall structure, such as the navigation pattern.LayoutBuildersuppliesconstraints.maxWidth, the space the parent gives this widget. Use it for decisions about a part of the screen, such as how a card or a panel lays itself out.
The difference shows up when a widget is placed inside something narrower than the window. A card inside a 300-pixel side panel on a 1200-pixel window sees a compact width through LayoutBuilder and an expanded width through MediaQuery. The first answer matches the space the card has.
The next widget picks its layout from the constraints it receives.
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 switch expression has no default branch. Dart checks that it covers every value of the enum. If you later add WindowSize.large, the compiler reports an error at each switch that needs a new case. That is the main reason to use an enum instead of bare numbers.
Boundary behavior
Check the edges, because off-by-one mistakes sit at the boundaries. With the helper above:
| Width | Result |
|---|---|
| 599.9 | compact |
| 600 | medium |
| 839.9 | medium |
| 840 | expanded |
Because the helper is a pure function, these cases make a quick unit test.
import 'package:flutter_test/flutter_test.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;
}
void main() {
test('window size boundaries', () {
expect(windowSizeFor(599.9), WindowSize.compact);
expect(windowSizeFor(600), WindowSize.medium);
expect(windowSizeFor(839.9), WindowSize.medium);
expect(windowSizeFor(840), WindowSize.expanded);
});
}In a real project the enum and helper live in one file that both the app and the test import. They are repeated here so the test runs as shown.
Common errors with breakpoints
Using height or orientation as a stand-in. Orientation tells you whether width exceeds height, not how much room you have. A landscape phone is wider than a portrait tablet's narrow pane in some cases and narrower in others. Base the decision on width.
Using the device's physical size. Windows resize on desktop and web, and mobile apps can run in split-screen or floating windows. Read the window or the constraints, never the screen.
Checking MediaQuery inside a nested widget. The widget then reacts to the window instead of its available space. If it behaves badly in a side panel, switch to LayoutBuilder.
Calling MediaQuery.of(context) for width only. MediaQuery.of makes the widget rebuild whenever any media value changes, including the keyboard inset. MediaQuery.sizeOf(context) subscribes only to the size, so it rebuilds less often.
Changing state when the class changes. If a widget keeps state such as a selected item, a layout switch can rebuild the subtree. When the compact and expanded layouts use different widget types in the same position, Flutter discards the state of the old subtree. Keep selection state in a parent widget that sits above the switch, or give the shared child a GlobalKey or a PageStorageKey if its own state must survive.
When to add more breakpoints
Three classes are a starting point, not a rule. Add another range only when the design needs a different arrangement there, not because a new device exists. A reading column that grows too wide is better fixed with a ConstrainedBox and a maximum width than with a new class. Fewer classes mean fewer layouts to build and test.
Use the class for structural changes: how many panes, which navigation widget, how many columns of content. Use flexible widgets such as Expanded and Wrap for the smooth in-between sizing. Breakpoints handle the jumps and flexible layout handles the slopes.
Exercise
A product card sits in a 320-pixel-wide side panel inside a 1100-pixel-wide window. The card chooses its layout with windowSizeFor. Should it pass MediaQuery.sizeOf(context).width or constraints.maxWidth from a LayoutBuilder, and which WindowSize results in each case?
Show answer
It should use constraints.maxWidth from a LayoutBuilder, because the card must fit the space its parent gives it. That width is 320 and gives WindowSize.compact. MediaQuery.sizeOf(context).width would be 1100 and give WindowSize.expanded, which would pick a wide layout for a narrow panel.
Exercise
What does windowSizeFor(840) return, and why does the order of the if statements in the helper matter?
Show answer
It returns WindowSize.expanded, because 840 is not less than 600 and not less than 840. The checks run from the smallest range upward, and each early return handles one range. If the checks were reordered, for example testing width < 840 first, widths under 600 would wrongly return medium.