Sub topic 9
Switching between a bottom bar and a navigation rail
This chapter assumes basic Dart, widget trees, Scaffold, Row, Column, and ListView. It also assumes you can run a Flutter app and understand Dart 3 switch expressions.
A navigation pattern is adaptive when it changes to fit the available window. A bottom NavigationBar works well on a narrow phone because it is easy to reach with a thumb. A NavigationRail uses the side of a wider window and leaves more vertical space for content. Both controls should represent the same destinations and update the same selected page.
Choose the layout from the window width
A breakpoint is a width at which the interface changes structure. Material 3 commonly uses 600 logical pixels as the boundary between compact and wider layouts. Logical pixels are Flutter's device-independent measurement unit.
Use MediaQuery.sizeOf when the decision depends on the whole app window:
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;
static const _pages = [
Text('Home'),
Text('Search'),
Text('Settings'),
];
@override
Widget build(BuildContext context) {
final useRail = MediaQuery.sizeOf(context).width >= 600;
final body = Center(child: _pages[_index]);
return Scaffold(
body: useRail
? Row(
children: [
NavigationRail(
selectedIndex: _index,
onDestinationSelected: (index) {
setState(() => _index = index);
},
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.settings),
label: Text('Settings'),
),
],
),
const VerticalDivider(width: 1),
Expanded(child: body),
],
)
: body,
bottomNavigationBar: useRail
? null
: NavigationBar(
selectedIndex: _index,
onDestinationSelected: (index) {
setState(() => _index = index);
},
destinations: const [
NavigationDestination(
icon: Icon(Icons.home),
label: 'Home',
),
NavigationDestination(
icon: Icon(Icons.search),
label: 'Search',
),
NavigationDestination(
icon: Icon(Icons.settings),
label: 'Settings',
),
],
),
);
}
}The _index field is shared by both navigation widgets. When the window crosses the breakpoint, the control changes but the selected destination remains the same. The Expanded widget is required in the rail layout so the page body receives the remaining horizontal space.
| Window width | Navigation control | Typical use |
|---|---|---|
| Less than 600 logical pixels | `NavigationBar` | Phone or narrow window |
| At least 600 logical pixels | `NavigationRail` | Tablet, desktop, or wide window |
Keep the breakpoint local when necessary
MediaQuery.sizeOf(context) reports the surrounding window size. Use LayoutBuilder instead when the shell might appear inside a smaller panel, split view, or reusable component. LayoutBuilder gives you the constraints offered by that widget's parent, so the choice reflects the space the shell actually receives.
Do not create two independent selected-index fields. That causes the rail and bar to disagree after a resize. Also avoid showing both controls at once. The same destinations should have the same order, labels, icons, and meaning.
Test both branches
Widget tests can set a simulated window size. Reset the view after the test so later tests do not inherit its dimensions.
testWidgets('uses a rail on a wide window', (tester) async {
tester.view.physicalSize = const Size(1200, 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);
});Add a second test below 600 pixels. Test the narrow and wide branches, rather than testing a particular phone model.
Exercise
Why should the selected destination be stored outside both navigation widgets?
Show answer
The widgets are alternatives shown at different widths. One shared index preserves the selected page when Flutter switches between them.