Sub topic 2
Reacting to available space with LayoutBuilder
Prerequisites
This chapter assumes you know Dart 3 syntax, can build widgets with StatelessWidget and StatefulWidget, and can use Row, Column, Scaffold, and ListView.
Why the available space matters
A Flutter widget does not know the size of the screen. It receives constraints from its parent: a minimum and maximum width and height. LayoutBuilder is a widget that hands those constraints to a builder function, so your layout can respond to the space it actually gets. That space may be a phone, a split-screen half, or a resized desktop window.
import 'package:flutter/material.dart';
class AdaptiveBody extends StatelessWidget {
const AdaptiveBody({super.key});
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) {
final wide = constraints.maxWidth >= 600;
return Text(wide ? 'Two panes' : 'One pane');
},
);
}
}Defining breakpoints with window-size classes
A breakpoint is a width where the layout changes. Material 3 defines three window-size classes: compact (under 600 logical pixels), medium (600 to 839), and expanded (840 or more). Put the boundaries in one place so every screen agrees.
enum WindowSize { compact, medium, expanded }
WindowSize windowSizeFor(double width) {
if (width < 600) return WindowSize.compact;
if (width < 840) return WindowSize.medium;
return WindowSize.expanded;
}Grids and lists that adjust their column count
SliverGridDelegateWithMaxCrossAxisExtent picks the column count for you. Each tile stays at most 200 logical pixels wide.
import 'package:flutter/material.dart';
class PhotoGrid extends StatelessWidget {
const PhotoGrid({super.key});
@override
Widget build(BuildContext context) {
return GridView.builder(
padding: const EdgeInsets.all(8),
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 200,
mainAxisSpacing: 8,
crossAxisSpacing: 8,
),
itemCount: 40,
itemBuilder: (context, index) =>
Card(child: Center(child: Text('Item $index'))),
);
}
}Safe areas and text scaling
Wrap content in SafeArea so it avoids notches, status bars, and system gesture areas. Users can also set a larger text size. Read MediaQuery.textScalerOf(context) before you fix a height, because a fixed-height box can clip scaled text.
> Keep heights flexible. A fixed height is the most common cause of clipped text.
Switching between bottom bar and navigation rail
The diagram shows the decision that the code below makes.
import 'package:flutter/material.dart';
class AdaptiveShell extends StatefulWidget {
const AdaptiveShell({super.key});
@override
State<AdaptiveShell> createState() => _AdaptiveShellState();
}
class _AdaptiveShellState extends State<AdaptiveShell> {
int _index = 0;
@override
Widget build(BuildContext context) {
final wide = MediaQuery.sizeOf(context).width >= 600;
final body = Center(child: Text('Page ${_index + 1}'));
return Scaffold(
body: wide
? Row(children: [
NavigationRail(
selectedIndex: _index,
onDestinationSelected: (i) => setState(() => _index = i),
destinations: const [
NavigationRailDestination(icon: Icon(Icons.home), label: Text('Home')),
NavigationRailDestination(icon: Icon(Icons.search), label: Text('Search')),
],
),
Expanded(child: body),
])
: body,
bottomNavigationBar: wide
? null
: NavigationBar(
selectedIndex: _index,
onDestinationSelected: (i) => setState(() => _index = i),
destinations: const [
NavigationDestination(icon: Icon(Icons.home), label: 'Home'),
NavigationDestination(icon: Icon(Icons.search), label: 'Search'),
],
),
);
}
}Exercise
A card sits inside a sidebar that takes 300 pixels of a 1000-pixel window. Should you choose its layout with MediaQuery.sizeOf or LayoutBuilder, and why?
Show answer
Use LayoutBuilder. The card's real space is its constraints, not the window width, so LayoutBuilder reports the 700 pixels left for the card.