Skip to content
Blog

Flutter BottomNavigationBar: Production Patterns, State Preservation, and Styling

Learn how to build production-ready navigation bars in Flutter with BottomNavigationBar, maintaining tab state using IndexedStack and state preservation patterns.

Published on • November 9, 2026

AI Assistant

The bottom navigation bar is the primary navigation pattern for mobile applications. While Flutter makes adding a BottomNavigationBar to a Scaffold straightforward, preserving page scroll position and form state across tab switches requires proper architectural choices.

Basic BottomNavigationBar Usage

The standard BottomNavigationBar accepts a list of BottomNavigationBarItem widgets, a current selectedIndex, and an onTap callback.

import 'package:flutter/material.dart';

class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  int _currentIndex = 0;

  final List<Widget> _pages = const [
    FeedTab(),
    SearchTab(),
    NotificationsTab(),
    ProfileTab(),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _pages[_currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,
        onTap: (index) {
          setState(() => _currentIndex = index);
        },
        type: BottomNavigationBarType.fixed, // Fixed for 4+ items
        selectedItemColor: Theme.of(context).primaryColor,
        unselectedItemColor: Colors.grey.shade600,
        items: const [
          BottomNavigationBarItem(
            icon: Icon(Icons.home_outlined),
            activeIcon: Icon(Icons.home),
            label: 'Home',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.search_outlined),
            activeIcon: Icon(Icons.search),
            label: 'Search',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.notifications_outlined),
            activeIcon: Icon(Icons.notifications),
            label: 'Alerts',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.person_outline),
            activeIcon: Icon(Icons.person),
            label: 'Profile',
          ),
        ],
      ),
    );
  }
}

Preserving Tab State with IndexedStack

Rebuilding _pages[_currentIndex] directly destroys the inactive tabs from the widget tree when switching pages, losing scroll positions and entered text.

Using an IndexedStack keeps all child pages alive in the widget hierarchy, maintaining their state and scroll offsets without re-executing expensive initializations.

Scaffold(
  body: IndexedStack(
    index: _currentIndex,
    children: _pages,
  ),
  bottomNavigationBar: BottomNavigationBar(/* ... */),
);

Modern Declarative Routing with GoRouter

For larger apps with sub-routes (e.g. tapping an item inside SearchTab pushes a detail screen while keeping the bottom navigation bar visible), standard BottomNavigationBar with IndexedStack is superseded by StatefulShellRoute in go_router.

// Example StatefulShellRoute setup with GoRouter
StatefulShellRoute.indexedStack(
  builder: (context, state, navigationShell) {
    return Scaffold(
      body: navigationShell,
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: navigationShell.currentIndex,
        onTap: (index) {
          navigationShell.goBranch(
            index,
            initialLocation: index == navigationShell.currentIndex,
          );
        },
        items: const [/* ... */],
      ),
    );
  },
  branches: [
    StatefulShellBranch(routes: [/* Feed Routes */]),
    StatefulShellBranch(routes: [/* Search Routes */]),
  ],
)

Material 3 NavigationBar Alternative

In Material 3 design, NavigationBar (from flutter/material.dart) replaces BottomNavigationBar with pill-shaped active indicators and built-in animation standards.

NavigationBar(
  selectedIndex: _currentIndex,
  onDestinationSelected: (index) {
    setState(() => _currentIndex = index);
  },
  destinations: const [
    NavigationDestination(icon: Icon(Icons.home), label: 'Home'),
    NavigationDestination(icon: Icon(Icons.search), label: 'Search'),
    NavigationDestination(icon: Icon(Icons.person), label: 'Profile'),
  ],
)

Takeaway: Choose BottomNavigationBar with IndexedStack for simple state retention, or utilize Material 3 NavigationBar and GoRouter’s StatefulShellRoute for enterprise deep-linking and tab isolation.