0
0
Fluttermobile~20 mins

Nested navigation in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 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.