Bird
Raised Fist0
Fluttermobile~20 mins

Nested navigation in Flutter - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Nested Navigation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ navigation
intermediate
2:00remaining
Understanding Nested Navigator Widget

What will be the visible screen when this Flutter app runs?

Flutter
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Navigator(
          onGenerateRoute: (settings) {
            return MaterialPageRoute(
              builder: (context) => Center(child: Text('Nested Screen')),
            );
          },
        ),
      ),
    );
  }
}
AA blank white screen with no text
BA screen showing the default MaterialApp home widget
CAn error screen due to missing routes
DA screen showing the text 'Nested Screen' centered
Attempts:
2 left
💡 Hint

Look at what the Navigator's onGenerateRoute returns as the widget.

❓ ui_behavior
intermediate
2:00remaining
Behavior of Back Button in Nested Navigation

Given a nested Navigator inside a Scaffold, what happens when the Android back button is pressed if the nested Navigator has no pages to pop?

Flutter
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Navigator(
          onGenerateRoute: (settings) {
            return MaterialPageRoute(
              builder: (context) => Center(child: Text('Only Page')),
            );
          },
        ),
      ),
    );
  }
}
AThe back button pops the outer Navigator or closes the app if none
BThe back button does nothing because nested Navigator handles it internally
CThe back button pops the nested Navigator's only page and shows a blank screen
DThe app closes because the nested Navigator cannot pop any page
Attempts:
2 left
💡 Hint

Think about how Flutter handles back button events when nested navigators cannot pop.

❓ lifecycle
advanced
2:30remaining
State Preservation in Nested Navigators

In a Flutter app with a nested Navigator inside a tab, what happens to the nested Navigator's stack when switching tabs?

Flutter
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;

  final List<Widget> _tabs = [
    Navigator(
      key: GlobalKey<NavigatorState>(),
      onGenerateRoute: (settings) => MaterialPageRoute(
        builder: (context) => Scaffold(body: Center(child: Text('Tab 1 Root'))),
      ),
    ),
    Navigator(
      key: GlobalKey<NavigatorState>(),
      onGenerateRoute: (settings) => MaterialPageRoute(
        builder: (context) => Scaffold(body: Center(child: Text('Tab 2 Root'))),
      ),
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: IndexedStack(
          index: _selectedIndex,
          children: _tabs,
        ),
        bottomNavigationBar: BottomNavigationBar(
          currentIndex: _selectedIndex,
          onTap: (index) => setState(() => _selectedIndex = index),
          items: const [
            BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Tab 1'),
            BottomNavigationBarItem(icon: Icon(Icons.settings), label: 'Tab 2'),
          ],
        ),
      ),
    );
  }
}
AThe nested Navigator stack is lost and causes an error on return
BThe nested Navigator stack resets to root when switching tabs
CThe nested Navigator stack is preserved when switching tabs
DThe nested Navigator stack duplicates pages when switching tabs
Attempts:
2 left
💡 Hint

Consider how IndexedStack keeps widgets alive in Flutter.

📝 Syntax
advanced
2:30remaining
Correct Syntax for Nested Navigator with Routes

Which option correctly defines a nested Navigator with named routes in Flutter?

Flutter
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: NestedNavigator(),
      ),
    );
  }
}

class NestedNavigator extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Navigator(
      // ???
    );
  }
}
AinitialRoute: '/home', onGenerateRoute: (settings) => MaterialPageRoute(builder: (context) => HomeScreen()),
BinitialRoute: '/home', routes: {'/home': (context) => HomeScreen(), '/details': (context) => DetailsScreen()},
Croutes: {'/home': (context) => HomeScreen(), '/details': (context) => DetailsScreen()}, onGenerateRoute: (settings) => null,
DonGenerateRoute: (settings) => MaterialPageRoute(builder: (context) => HomeScreen()), routes: {'/home': (context) => HomeScreen()},
Attempts:
2 left
💡 Hint

Check the Navigator constructor parameters for named routes and initialRoute.

🔧 Debug
expert
3:00remaining
Debugging Nested Navigator State Loss

A Flutter app uses nested Navigators inside tabs. When switching tabs, the nested Navigator loses its navigation stack and resets to root. What is the most likely cause?

AUsing a new GlobalKey for the nested Navigator each time the tab builds
BKeeping the nested Navigator widgets alive with AutomaticKeepAliveClientMixin
CUsing IndexedStack to hold tab views instead of switching widgets directly
DUsing MaterialApp instead of Navigator for nested navigation
Attempts:
2 left
💡 Hint

Think about how Flutter identifies widgets and preserves their state.

Practice

(1/5)
1. What is the main purpose of nested navigation in a Flutter app?
easy
A. To manage navigation inside a specific part of the app separately
B. To replace the main Navigator of the app
C. To create multiple apps inside one Flutter project
D. To disable navigation on certain screens

Solution

  1. Step 1: Understand nested navigation concept

    Nested navigation allows managing navigation stacks inside a part of the app independently from the main app navigation.
  2. Step 2: Identify the purpose

    This helps keep navigation organized and user-friendly by isolating navigation flows.
  3. Final Answer:

    To manage navigation inside a specific part of the app separately -> Option A
  4. Quick Check:

    Nested navigation = Manage part navigation separately [OK]
Hint: Nested navigation isolates navigation flows inside app parts [OK]
Common Mistakes:
  • Thinking nested navigation replaces the main Navigator
  • Confusing nested navigation with multiple apps
  • Assuming nested navigation disables navigation
2. Which Flutter widget is essential to implement nested navigation?
easy
A. MaterialApp
B. Container
C. Scaffold
D. Navigator

Solution

  1. Step 1: Identify widget for navigation

    The Navigator widget manages a stack of routes and is essential for navigation in Flutter.
  2. Step 2: Confirm nested navigation use

    Nested navigation uses a Navigator with a unique key inside a widget subtree.
  3. Final Answer:

    Navigator -> Option D
  4. Quick Check:

    Navigator widget = navigation management [OK]
Hint: Navigator widget controls navigation stacks [OK]
Common Mistakes:
  • Choosing MaterialApp which is for app-wide setup
  • Selecting Scaffold which is for layout
  • Picking Container which is a basic layout widget
3. Given this nested Navigator code snippet, what screen will be shown first inside the nested navigator?
Navigator(
  key: _nestedKey,
  onGenerateRoute: (settings) {
    return MaterialPageRoute(
      builder: (context) => NestedHomeScreen(),
    );
  },
)
medium
A. NestedHomeScreen
B. The main app's home screen
C. An error because onGenerateRoute is missing
D. A blank screen

Solution

  1. Step 1: Analyze onGenerateRoute usage

    The onGenerateRoute callback returns a MaterialPageRoute with NestedHomeScreen as the builder.
  2. Step 2: Determine initial screen

    Since this is the only route generated, NestedHomeScreen will be the first screen shown inside the nested Navigator.
  3. Final Answer:

    NestedHomeScreen -> Option A
  4. Quick Check:

    onGenerateRoute returns NestedHomeScreen [OK]
Hint: onGenerateRoute builder shows initial nested screen [OK]
Common Mistakes:
  • Confusing main app screen with nested screen
  • Assuming missing route causes error
  • Expecting blank screen without routes
4. What is wrong with this nested Navigator code?
Navigator(
  onGenerateRoute: (settings) {
    return MaterialPageRoute(
      builder: (context) => NestedScreen(),
    );
  },
)
It causes navigation issues inside the app.
medium
A. Not wrapping Navigator inside a Scaffold
B. Using MaterialPageRoute instead of CupertinoPageRoute
C. Missing a unique key for the nested Navigator
D. Not defining initialRoute property

Solution

  1. Step 1: Check Navigator setup

    For nested navigation, Navigator should have a unique GlobalKey to manage its own stack separately.
  2. Step 2: Identify missing key

    This code lacks a key, which can cause navigation conflicts or unexpected behavior.
  3. Final Answer:

    Missing a unique key for the nested Navigator -> Option C
  4. Quick Check:

    Nested Navigator needs unique key [OK]
Hint: Always assign a unique key to nested Navigator [OK]
Common Mistakes:
  • Thinking route type causes issues
  • Believing Scaffold is mandatory for Navigator
  • Assuming initialRoute is required
5. You want to create a tabbed interface where each tab has its own navigation stack. Which approach correctly implements nested navigation for this use case?
hard
A. Use a single Navigator for the whole app and switch tabs by pushing routes
B. Use a Navigator with a unique key inside each tab widget to manage its own stack
C. Use multiple MaterialApp widgets, one per tab
D. Use IndexedStack without any Navigator inside tabs

Solution

  1. Step 1: Understand tab navigation needs

    Each tab should maintain its own navigation stack independently to allow back navigation inside tabs.
  2. Step 2: Apply nested Navigator per tab

    Using a Navigator with a unique key inside each tab widget isolates navigation stacks correctly.
  3. Step 3: Evaluate other options

    Single Navigator can't isolate stacks; multiple MaterialApps is heavy and incorrect; IndexedStack alone doesn't handle navigation.
  4. Final Answer:

    Use a Navigator with a unique key inside each tab widget to manage its own stack -> Option B
  5. Quick Check:

    Nested Navigator per tab = isolated stacks [OK]
Hint: Assign unique Navigator per tab for independent navigation [OK]
Common Mistakes:
  • Using one Navigator for all tabs causing stack conflicts
  • Trying multiple MaterialApps which is not recommended
  • Ignoring Navigator inside tabs and losing navigation control
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard