No extra packages or backend. Paste the complete example into lib/main.dart in a Flutter app.
View complete source
import 'package:flutter/material.dart';
/// Compare local shell width, not device type or orientation.
bool useNavigationRail(double width) => width >= 600;
void main() => runApp(
MaterialApp(
theme: ThemeData(useMaterial3: true, colorSchemeSeed: Colors.indigo),
home: const AdaptiveNavigationExample(),
),
);
class AdaptiveNavigationExample extends StatefulWidget {
const AdaptiveNavigationExample({super.key});
@override
State<AdaptiveNavigationExample> createState() => _AdaptiveNavigationState();
}
class _AdaptiveNavigationState extends State<AdaptiveNavigationExample> {
static const destinations = [
(label: 'Overview', icon: Icons.dashboard_outlined),
(label: 'Draft', icon: Icons.edit_note),
];
int selectedIndex = 0;
final draft = TextEditingController();
@override
void dispose() {
draft.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) => LayoutBuilder(
builder: (context, constraints) {
final rail = useNavigationRail(constraints.maxWidth);
void select(int index) => setState(() => selectedIndex = index);
return Scaffold(
appBar: AppBar(title: const Text('Adaptive navigation')),
body: SafeArea(
bottom: rail,
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
if (rail)
NavigationRail(
selectedIndex: selectedIndex,
onDestinationSelected: select,
labelType: NavigationRailLabelType.all,
destinations: [
for (final item in destinations)
NavigationRailDestination(
icon: Icon(item.icon),
label: Text(item.label),
),
],
),
Expanded(
key: const ValueKey('destination-content'),
child: IndexedStack(
index: selectedIndex,
children: [
const Center(child: Text('Pick Draft to enter a note.')),
ListView(
padding: const EdgeInsets.all(16),
children: [
TextField(
key: const ValueKey('adaptive-draft'),
controller: draft,
decoration: const InputDecoration(
labelText: 'Draft note',
border: OutlineInputBorder(),
),
),
],
),
],
),
),
],
),
),
bottomNavigationBar: rail
? null
: NavigationBar(
selectedIndex: selectedIndex,
onDestinationSelected: select,
destinations: [
for (final item in destinations)
NavigationDestination(
icon: Icon(item.icon),
label: item.label,
),
],
),
);
},
);
}