Sub topic 13
Testing Layouts at Different Window Sizes
This chapter assumes basic Dart, widget trees, Scaffold, Row, Column, and ListView. You should also know how to run Flutter widget tests and use Dart 3 switch expressions.
Responsive layouts can fail even when they look correct at one device size. A widget test gives the app a known window size, pumps the widget tree, and checks which layout appears. This makes breakpoint behavior repeatable.
Test the boundary, not only the typical size
A layout usually changes at a breakpoint. Test sizes on both sides of that breakpoint and test the boundary itself when the exact comparison matters.
For example, this shell uses a NavigationBar below 600 logical pixels and a NavigationRail at 600 pixels or wider:
final useRail = MediaQuery.sizeOf(context).width >= 600;The test window uses physical pixels, so set the device pixel ratio to 1.0 when you want physical and logical sizes to match. Always restore the test view after the test. Otherwise, a later test can inherit the modified size.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
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('Settings'),
];
@override
Widget build(BuildContext context) {
final useRail = MediaQuery.sizeOf(context).width >= 600;
return Scaffold(
body: useRail
? Row(
children: [
NavigationRail(
selectedIndex: index,
onDestinationSelected: (value) {
setState(() => index = value);
},
destinations: const [
NavigationRailDestination(
icon: Icon(Icons.home),
label: Text('Home'),
),
NavigationRailDestination(
icon: Icon(Icons.search),
label: Text('Search'),
),
NavigationRailDestination(
icon: Icon(Icons.settings),
label: Text('Settings'),
),
],
),
Expanded(child: Center(child: pages[index])),
],
)
: Center(child: pages[index]),
bottomNavigationBar: useRail
? null
: NavigationBar(
selectedIndex: index,
onDestinationSelected: (value) {
setState(() => index = value);
},
destinations: const [
NavigationDestination(
icon: Icon(Icons.home),
label: 'Home',
),
NavigationDestination(
icon: Icon(Icons.search),
label: 'Search',
),
NavigationDestination(
icon: Icon(Icons.settings),
label: 'Settings',
),
],
),
);
}
}
void main() {
testWidgets('uses a rail at the wide breakpoint', (tester) async {
tester.view.physicalSize = const Size(600, 800);
tester.view.devicePixelRatio = 1.0;
addTearDown(tester.view.reset);
await tester.pumpWidget(const MaterialApp(home: AdaptiveShell()));
expect(find.byType(NavigationRail), findsOneWidget);
expect(find.byType(NavigationBar), findsNothing);
});
}Test several window classes
One test can cover the compact and expanded cases, but separate tests usually show failures more clearly. pumpWidget builds the widget tree. pump lets pending animations or callbacks run.
void setWindowSize(WidgetTester tester, double width, double height) {
tester.view.physicalSize = Size(width, height);
tester.view.devicePixelRatio = 1.0;
}
testWidgets('uses a bottom bar on a compact window', (tester) async {
setWindowSize(tester, 599, 800);
addTearDown(tester.view.reset);
await tester.pumpWidget(const MaterialApp(home: AdaptiveShell()));
expect(find.byType(NavigationBar), findsOneWidget);
expect(find.byType(NavigationRail), findsNothing);
});A useful test matrix records the expected result for each class:
| Logical width | Expected navigation |
|---|---|
| 375 | `NavigationBar` |
| 599 | `NavigationBar` |
| 600 | `NavigationRail` |
| 1200 | `NavigationRail` |
Test local constraints separately
A widget using LayoutBuilder responds to its parent’s constraints, not necessarily to the whole window. To test it accurately, place it inside a parent with the intended width. This catches errors where a child uses MediaQuery even though its actual panel is narrower.
Exercise
Why should a widget test call addTearDown(tester.view.reset) after changing the test window?
Show answer
The test view is shared by later tests. Resetting it prevents the changed size and device pixel ratio from affecting unrelated tests.