0
0
Fluttermobile~20 mins

Bottom navigation bar in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bottom Navigation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What happens when you tap a BottomNavigationBar item?

Given this Flutter code snippet for a BottomNavigationBar, what will be the visible result when the user taps the second item?

Flutter
BottomNavigationBar(
  currentIndex: _selectedIndex,
  onTap: (int index) {
    setState(() {
      _selectedIndex = index;
    });
  },
  items: const [
    BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
    BottomNavigationBarItem(icon: Icon(Icons.search), label: 'Search'),
    BottomNavigationBarItem(icon: Icon(Icons.person), label: 'Profile'),
  ],
)
AThe app crashes because _selectedIndex is not defined.
BNothing changes visually because onTap is not connected to any state update.
CAll items become highlighted simultaneously.
DThe second item icon and label become highlighted, and the UI updates to show the Search page content.
Attempts:
2 left
💡 Hint

Think about what setState does and how currentIndex controls the highlight.

lifecycle
intermediate
1:30remaining
Why use setState inside onTap of BottomNavigationBar?

In Flutter, why is it important to call setState inside the onTap callback of a BottomNavigationBar?

ABecause setState disables the BottomNavigationBar temporarily.
BBecause setState tells Flutter to rebuild the widget with updated state, reflecting the selected tab visually.
CBecause setState automatically saves the app state to disk.
DBecause setState prevents the app from freezing when tapping items.
Attempts:
2 left
💡 Hint

Think about how Flutter knows to update the screen when data changes.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this BottomNavigationBar code

What is the syntax error in this BottomNavigationBar snippet?

Flutter
BottomNavigationBar(
  currentIndex: _selectedIndex,
  onTap: (index) => setState(() => _selectedIndex = index),
  items: [
    BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
    BottomNavigationBarItem(icon: Icon(Icons.search), label: 'Search'),
  ],
)
AMissing comma after currentIndex: _selectedIndex
BonTap callback syntax is invalid
CBottomNavigationBarItem requires a title property instead of label
DItems list should be a Map, not a List
Attempts:
2 left
💡 Hint

Check punctuation between parameters.

navigation
advanced
2:30remaining
How to preserve state when switching tabs with BottomNavigationBar?

Which approach best preserves the state of each tab's content when switching tabs using BottomNavigationBar?

AUse IndexedStack to keep all tab widgets alive and switch visibility based on selected index.
BUse Navigator.push to open a new page for each tab selection.
CRebuild the tab content widget every time the tab changes to refresh the UI.
DUse a single widget and change its content dynamically without preserving previous state.
Attempts:
2 left
💡 Hint

Think about how to keep widgets in memory but only show one at a time.

🔧 Debug
expert
3:00remaining
Why does BottomNavigationBar not update highlight on tap?

Given this Flutter code, why does tapping BottomNavigationBar items not update the highlighted item?

Flutter
int _selectedIndex = 0;

Widget build(BuildContext context) {
  return Scaffold(
    bottomNavigationBar: BottomNavigationBar(
      currentIndex: _selectedIndex,
      onTap: (index) {
        _selectedIndex = index;
      },
      items: const [
        BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
        BottomNavigationBarItem(icon: Icon(Icons.search), label: 'Search'),
      ],
    ),
  );
}
ABecause BottomNavigationBar requires a key to update the selected item.
BBecause currentIndex must be a final variable, not a mutable one.
CBecause setState is not called after updating _selectedIndex, so UI does not rebuild.
DBecause onTap callback must return a boolean to confirm selection.
Attempts:
2 left
💡 Hint

Think about how Flutter knows to redraw widgets when state changes.