Adaptive Flutter navigation

Use a bottom navigation bar below 600 logical pixels and a side rail at 600 or wider. Keep the selected destination and unsaved input in the same state as the layout changes.

Try the live demo

Choose Draft, type a note, then switch preview widths. Your note should still be there.

Open full-screen demo

Copy the Dart example

No extra packages or backend. Paste the complete example into lib/main.dart in a Flutter app.

Download Dart
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,
                    ),
                ],
              ),
      );
    },
  );
}

Reuse the breakpoint helper

bool useNavigationRail(double width) => width >= 600;

Call it with constraints.maxWidth inside LayoutBuilder. Keep the selected destination and text controller above the navigation switch.

Get the matching AI prompt to adapt the pattern to your Flutter project.