A bottom navigation bar helps users switch between main sections of an app quickly. It stays at the bottom of the screen for easy access.
Bottom navigation bar in Flutter
BottomNavigationBar(
items: [
BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
BottomNavigationBarItem(icon: Icon(Icons.search), label: 'Search'),
BottomNavigationBarItem(icon: Icon(Icons.person), label: 'Profile'),
],
currentIndex: selectedIndex,
onTap: (int index) {
setState(() {
selectedIndex = index;
});
},
)items is a list of buttons shown in the bar.
currentIndex shows which item is active.
onTap handles taps to change the active item.
BottomNavigationBar(
items: [
BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
BottomNavigationBarItem(icon: Icon(Icons.settings), label: 'Settings'),
],
currentIndex: 0,
onTap: (index) {},
)BottomNavigationBar(
items: [
BottomNavigationBarItem(icon: Icon(Icons.chat), label: 'Chat'),
BottomNavigationBarItem(icon: Icon(Icons.notifications), label: 'Alerts'),
BottomNavigationBarItem(icon: Icon(Icons.person), label: 'Profile'),
],
currentIndex: selectedIndex,
onTap: (index) {
setState(() {
selectedIndex = index;
});
},
backgroundColor: Colors.blueGrey,
selectedItemColor: Colors.white,
unselectedItemColor: Colors.grey,
)This app shows a bottom navigation bar with three tabs: Home, Search, and Profile. Tapping each tab changes the main content area to show the selected page's name.
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatefulWidget { @override State<MyApp> createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { int _selectedIndex = 0; static const List<Widget> _pages = <Widget>[ Center(child: Text('Home Page', style: TextStyle(fontSize: 24))), Center(child: Text('Search Page', style: TextStyle(fontSize: 24))), Center(child: Text('Profile Page', style: TextStyle(fontSize: 24))), ]; void _onItemTapped(int index) { setState(() { _selectedIndex = index; }); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('Bottom Navigation Bar Example')), body: _pages[_selectedIndex], bottomNavigationBar: BottomNavigationBar( items: const <BottomNavigationBarItem>[ BottomNavigationBarItem( icon: Icon(Icons.home), label: 'Home', ), BottomNavigationBarItem( icon: Icon(Icons.search), label: 'Search', ), BottomNavigationBarItem( icon: Icon(Icons.person), label: 'Profile', ), ], currentIndex: _selectedIndex, selectedItemColor: Colors.blue, onTap: _onItemTapped, ), ), ); } }
Keep the number of items between 3 and 5 for best usability.
Use clear icons and labels so users understand each tab.
Remember to update the currentIndex when a tab is tapped to show the active page.
Bottom navigation bars help users switch main app sections easily.
Use BottomNavigationBar widget with items, currentIndex, and onTap.
Update the selected index state to change the active tab and content.
Practice
BottomNavigationBar in a Flutter app?Solution
Step 1: Understand the role of BottomNavigationBar
The BottomNavigationBar widget is designed to help users navigate between main app sections by showing tabs at the bottom.Step 2: Compare with other UI elements
Options B, C, and D describe different widgets or behaviors not related to bottom navigation bars.Final Answer:
To let users switch between main sections of the app easily -> Option AQuick Check:
BottomNavigationBar purpose = navigation tabs [OK]
- Confusing BottomNavigationBar with popup menus
- Thinking it creates floating buttons
- Assuming it displays vertical lists
Solution
Step 1: Check the correct property for items
The BottomNavigationBar uses theitemsproperty with a list ofBottomNavigationBarItemwidgets.Step 2: Verify the syntax of items
BottomNavigationBar(items: [BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'), BottomNavigationBarItem(icon: Icon(Icons.search), label: 'Search')]) correctly usesitemswithBottomNavigationBarItemincludingiconandlabel.Final Answer:
BottomNavigationBar(items: [BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'), BottomNavigationBarItem(icon: Icon(Icons.search), label: 'Search')]) -> Option DQuick Check:
Use items with BottomNavigationBarItem [OK]
- Using children instead of items
- Using tabs or buttons properties which don't exist
- Omitting label in BottomNavigationBarItem
_selectedIndex after tapping the second tab?int _selectedIndex = 0;
BottomNavigationBar(
currentIndex: _selectedIndex,
onTap: (int index) {
_selectedIndex = index;
},
items: const [
BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
BottomNavigationBarItem(icon: Icon(Icons.settings), label: 'Settings'),
],
)Solution
Step 1: Understand onTap behavior
TheonTapcallback receives the tapped tab's index and assigns it to_selectedIndex.Step 2: Identify tapped tab index
The second tab has index 1 (indexing starts at 0), so_selectedIndexbecomes 1.Final Answer:
1 -> Option AQuick Check:
Tapped tab index = _selectedIndex = 1 [OK]
- Assuming index starts at 1
- Not updating _selectedIndex on tap
- Confusing currentIndex with tapped index
int _selectedIndex = 0;
BottomNavigationBar(
items: const [
BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
BottomNavigationBarItem(icon: Icon(Icons.settings), label: 'Settings'),
],
onTap: (int index) {
_selectedIndex = index;
},
)Solution
Step 1: Check required properties for BottomNavigationBar
ThecurrentIndexproperty is needed to tell the widget which tab is active.Step 2: Identify missing currentIndex usage
WithoutcurrentIndex, the UI won't update to show the selected tab visually.Final Answer:
Missing currentIndex property to reflect selected tab -> Option BQuick Check:
currentIndex must be set for selected tab highlight [OK]
- Forgetting currentIndex causes no visual update
- Thinking onTap must return a value
- Using deprecated title instead of label
Solution
Step 1: Understand state management for BottomNavigationBar
To update UI on tab tap, you must store selected tab index in state and update it properly.Step 2: Use StatefulWidget and setState to update UI
Use a StatefulWidget with an int _selectedIndex state; update _selectedIndex in onTap; use _selectedIndex to show different widgets in build method correctly uses StatefulWidget, updates _selectedIndex inside onTap, and calls setState to rebuild UI with new content.Final Answer:
Use a StatefulWidget with an int _selectedIndex state; update _selectedIndex in onTap; use _selectedIndex to show different widgets in build method -> Option CQuick Check:
StatefulWidget + setState + _selectedIndex update = correct [OK]
- Using StatelessWidget and expecting UI to update
- Not calling setState after changing _selectedIndex
- Updating _selectedIndex without rebuilding UI
Ready to prove it?
Pass the test and this pattern moves unknown
