Flutter Responsive Layouts
One Flutter codebase can run on a small phone, a large tablet, a desktop window and a browser tab. A responsive layout adapts to the space it gets: one column on a phone, two panes on a tablet, a navigation rail on desktop. Flutter gives you two main tools to measure that space: MediaQuery and LayoutBuilder.
MediaQuery: the whole screen
MediaQuery.sizeOf(context) returns the size of the app window in logical pixels. Other helpers give you the orientation, the text scale the user chose, and the "safe" padding around notches.
class ScreenInfo extends StatelessWidget {
const ScreenInfo({super.key});
@override
Widget build(BuildContext context) {
final size = MediaQuery.sizeOf(context);
final orientation = MediaQuery.orientationOf(context);
debugPrint('Size: ${size.width} x ${size.height}, $orientation');
return Center(
child: Text('${size.width.toInt()} × ${size.height.toInt()}',
style: const TextStyle(fontSize: 28)),
);
}
}Size: 360.0 x 640.0, Orientation.portrait
Tip: Prefer
MediaQuery.sizeOf(context)overMediaQuery.of(context).size. The specific version only rebuilds your widget when the size changes, not when unrelated values (like the keyboard insets) change.
LayoutBuilder: the space you were given
MediaQuery describes the whole window, but a widget inside a side panel only has part of it. LayoutBuilder tells you the constraints from your parent, which makes it ideal for reusable components.
class ProductGrid extends StatelessWidget {
const ProductGrid({super.key});
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) {
final columns = switch (constraints.maxWidth) {
< 600 => 2,
< 900 => 3,
_ => 5,
};
return GridView.count(
crossAxisCount: columns,
padding: const EdgeInsets.all(8),
mainAxisSpacing: 8,
crossAxisSpacing: 8,
children: [
for (var i = 1; i <= 10; i++)
Card(child: Center(child: Text('Item $i'))),
],
);
},
);
}
}The Dart 3 switch expression with relational patterns (< 600) keeps breakpoint logic short and readable.
Breakpoints
There is no single correct set of breakpoints, but Material 3's window size classes are a good default:
| Window class | Width | Typical layout |
|---|---|---|
| Compact | < 600 | Single column, bottom navigation bar |
| Medium | 600 – 839 | Two columns or list + detail, navigation rail |
| Expanded | ≥ 840 | Multi-pane, permanent navigation drawer or rail |
Switching navigation patterns
A common responsive pattern swaps a NavigationBar at the bottom (phones) for a NavigationRail at the side (tablets and desktop).
class AdaptiveShell extends StatefulWidget {
const AdaptiveShell({super.key});
@override
State<AdaptiveShell> createState() => _AdaptiveShellState();
}
class _AdaptiveShellState extends State<AdaptiveShell> {
int _index = 0;
static const _pages = [Text('Home'), Text('Search'), Text('Profile')];
@override
Widget build(BuildContext context) {
final wide = MediaQuery.sizeOf(context).width >= 600;
final body = Center(child: _pages[_index]);
if (wide) {
return Scaffold(
body: Row(children: [
NavigationRail(
selectedIndex: _index,
onDestinationSelected: (i) => setState(() => _index = i),
labelType: NavigationRailLabelType.all,
destinations: const [
NavigationRailDestination(icon: Icon(Icons.home), label: Text('Home')),
NavigationRailDestination(icon: Icon(Icons.search), label: Text('Search')),
NavigationRailDestination(icon: Icon(Icons.person), label: Text('Profile')),
],
),
const VerticalDivider(width: 1),
Expanded(child: body),
]),
);
}
return Scaffold(
body: body,
bottomNavigationBar: NavigationBar(
selectedIndex: _index,
onDestinationSelected: (i) => setState(() => _index = i),
destinations: const [
NavigationDestination(icon: Icon(Icons.home), label: 'Home'),
NavigationDestination(icon: Icon(Icons.search), label: 'Search'),
NavigationDestination(icon: Icon(Icons.person), label: 'Profile'),
],
),
);
}
}More responsive tips
- Use
Expanded,FlexibleandWrapso content stretches and wraps instead of overflowing. - Limit line length on wide screens:
Center(child: ConstrainedBox(constraints: BoxConstraints(maxWidth: 700), child: …)). - Wrap screens in
SafeAreato avoid notches and system bars. - Respect the user's font size — avoid fixed heights for text containers.
- Test by resizing a desktop or web build window; it's faster than switching devices.
Note: Responsive means adapting to size; adaptive means adapting to the platform (e.g. Cupertino widgets on iOS, mouse hover on desktop). Good apps do both.
Test yourself
Which tool tells you the constraints given by a widget's parent?
- MediaQuery
- LayoutBuilder
- Theme
- Navigator
Why prefer MediaQuery.sizeOf(context) over MediaQuery.of(context).size?
- It returns pixels instead of logical pixels
- It only rebuilds when the size changes
- It works without a context
- It is the only one that works on web
On wide screens, which widget commonly replaces a bottom NavigationBar?
- NavigationRail
- TabBar
- BottomSheet
- Drawer only on phones