0
0
Fluttermobile~20 mins

Tab navigation in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Tab Navigation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What is the output of this Flutter TabBar code?

Consider this Flutter widget with a TabBar and TabBarView. What will the user see when the app runs?

Flutter
DefaultTabController(
  length: 2,
  child: Scaffold(
    appBar: AppBar(
      bottom: TabBar(
        tabs: [
          Tab(icon: Icon(Icons.home)),
          Tab(icon: Icon(Icons.settings)),
        ],
      ),
      title: Text('Tabs Example'),
    ),
    body: TabBarView(
      children: [
        Center(child: Text('Home Page')),
        Center(child: Text('Settings Page')),
      ],
    ),
  ),
)
AThe app shows an AppBar with two tabs: Home and Settings icons. Tapping each tab switches the main content between 'Home Page' and 'Settings Page' text.
BThe app shows two tabs but tapping them does not change the content; the body stays empty.
CThe app shows tabs but the icons are missing and only text labels appear.
DThe app crashes because TabBarView children count does not match TabBar tabs count.
Attempts:
2 left
💡 Hint

Check how many tabs and TabBarView children are defined and what widgets they contain.

lifecycle
intermediate
2:00remaining
What happens when switching tabs in this Flutter app?

Given this Flutter code with TabBarView and stateful children, what lifecycle behavior occurs when switching tabs?

Flutter
DefaultTabController(
  length: 2,
  child: Scaffold(
    appBar: AppBar(
      bottom: TabBar(tabs: [Tab(text: 'A'), Tab(text: 'B')]),
    ),
    body: TabBarView(
      children: [
        StatefulWidgetA(),
        StatefulWidgetB(),
      ],
    ),
  ),
)

class StatefulWidgetA extends StatefulWidget {
  @override
  State<StatefulWidgetA> createState() => _StatefulWidgetAState();
}

class _StatefulWidgetAState extends State<StatefulWidgetA> {
  @override
  void initState() {
    super.initState();
    print('A initState');
  }
  @override
  Widget build(BuildContext context) {
    return Center(child: Text('Widget A'));
  }
}
ASwitching tabs disposes the previous tab widget and recreates it fresh with initState.
BSwitching tabs causes initState to run again for the tab being shown each time.
CBoth StatefulWidgetA and StatefulWidgetB initState run once at app start; switching tabs does not recreate them.
DOnly the first tab's initState runs; the second tab's initState never runs.
Attempts:
2 left
💡 Hint

Think about how TabBarView keeps its children alive by default.

navigation
advanced
2:00remaining
Which option correctly updates the selected tab programmatically?

You want to switch to the second tab in a DefaultTabController from code (not user tap). Which code snippet does this correctly?

Flutter
DefaultTabController(
  length: 3,
  child: Scaffold(
    appBar: AppBar(
      bottom: TabBar(tabs: [Tab(text: 'One'), Tab(text: 'Two'), Tab(text: 'Three')]),
    ),
    body: TabBarView(children: [Text('1'), Text('2'), Text('3')]),
  ),
)
ADefaultTabController.of(context)?.animateTo(1);
BTabController.of(context).index = 1;
CDefaultTabController.of(context).setIndex(1);
DTabBarController.of(context)?.animateToIndex(1);
Attempts:
2 left
💡 Hint

Look for the official method to animate tab changes on DefaultTabController.

📝 Syntax
advanced
2:00remaining
What error does this Flutter TabBar code produce?

Analyze this Flutter code snippet. What error will it cause?

Flutter
DefaultTabController(
  length: 2,
  child: Scaffold(
    appBar: AppBar(
      bottom: TabBar(
        tabs: [
          Tab(text: 'Tab 1'),
        ],
      ),
      title: Text('Example'),
    ),
    body: TabBarView(
      children: [
        Center(child: Text('Page 1')),
        Center(child: Text('Page 2')),
      ],
    ),
  ),
)
AThe app shows two tabs but tapping the second tab does nothing.
BThe app runs fine but only shows the first tab and page.
CThe app crashes with a null pointer exception.
DFlutter throws an assertion error because TabBar tabs count (1) does not match TabBarView children count (2).
Attempts:
2 left
💡 Hint

Check the number of tabs vs the number of TabBarView children.

🔧 Debug
expert
3:00remaining
Why does this Flutter TabBarView not update when tapping tabs?

Given this Flutter code, tapping tabs does not change the displayed page. What is the cause?

Flutter
class MyTabs extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        bottom: TabBar(
          tabs: [Tab(text: 'A'), Tab(text: 'B')],
        ),
        title: Text('Tabs'),
      ),
      body: TabBarView(
        children: [
          Center(child: Text('Page A')),
          Center(child: Text('Page B')),
        ],
      ),
    );
  }
}
AThe TabBar tabs list is empty, so no tabs appear to tap.
BNo DefaultTabController is provided, so TabBar and TabBarView have no controller to sync tabs and pages.
CThe TabBarView children are not wrapped in StatefulWidgets, so they don't update.
DThe Scaffold is missing a body property, so TabBarView is not shown.
Attempts:
2 left
💡 Hint

Check if a controller is managing the tabs.