What is BottomNavigationBar?
BottomNavigationBar is a widget that displays a row of icons and labels at the bottom of the screen. It's commonly used for navigating between the top‑level views of an app (e.g., home, search, profile). The selected item is visually highlighted, and when the user taps an item, the app updates the displayed content. This widget is a fundamental part of Material Design and provides a consistent navigation experience on mobile.
Basic Implementation
The simplest use involves a StatefulWidget that holds the current selected index. You provide a list of BottomNavigationBarItem widgets to the items property, and set currentIndex to the selected one. The onTap callback updates the index and (usually) the content.
Customizing Appearance
BottomNavigationBar offers several properties to control its look:
type:BottomNavigationBarType.fixed(all items visible) orshifting(selected item expands; usually for 3+ items).
selectedItemColor: Color of the selected icon and label.
unselectedItemColor: Color of unselected items.
backgroundColor: Background color of the bar.
elevation: Elevation (shadow) of the bar.
showSelectedLabelsandshowUnselectedLabels: Control label visibility.
iconSize: Size of the icons.
Handling Back Navigation with IndexedStack
When using multiple pages, each page might have its own navigation stack. A common approach is to use an IndexedStack to keep all pages alive, so their navigation state is preserved when switching tabs. The IndexedStack displays only the selected index but keeps all children in memory.
Combining with Routes (Navigator)
For more complex navigation where each tab has its own navigation stack, you can embed a Navigator inside each page. This allows per‑tab navigation history. A common pattern is to use a TabNavigator widget that holds a Navigator for that tab.
Common Mistakes
- Forgetting to call
setState: The bar won't update the selected index without callingsetStateinonTap.
- Forgetting to call
- Using too many items: For 4+ items, consider using
type: BottomNavigationBarType.shiftingfor better UX.
- Using too many items: For 4+ items, consider using
- Not handling back navigation: When the user presses the back button, you may want to exit the app or navigate to the previous screen inside the tab. Using
IndexedStackwithWillPopScopecan help.
- Not handling back navigation: When the user presses the back button, you may want to exit the app or navigate to the previous screen inside the tab. Using
- Changing the list of items after building: The list of items should be static; changing it requires a new
BottomNavigationBarto be built.
- Changing the list of items after building: The list of items should be static; changing it requires a new
- Over‑customizing: Making the bar too tall or using large icons can break the Material Design guidelines.
Best Practices
- Keep the number of items between 3 and 5. For 5+ items, consider alternatives like a drawer or tab bar.
- Provide both an icon and a label for each item to aid accessibility.
- Use
IndexedStackto preserve page state if needed.
- Use
- For deep navigation within a tab, use separate
Navigatorinstances per tab.
- For deep navigation within a tab, use separate
- Test on different screen sizes; on tablets, you might prefer a navigation rail.
Key Takeaways
BottomNavigationBaris used for top‑level navigation with icons and labels.
- The widget is typically placed inside
Scaffold'sbottomNavigationBarproperty.
- The widget is typically placed inside
- Use a
StatefulWidgetwithcurrentIndexandonTapto update the selected page.
- Use a
- Customize colors, icon size, and behavior with properties like
selectedItemColor,type, etc.
- Customize colors, icon size, and behavior with properties like
- Use
IndexedStackto keep pages alive when switching tabs.
- Use
- For per‑tab navigation history, consider using separate
Navigatorinstances.
- For per‑tab navigation history, consider using separate