What will be the visible screen when this Flutter app runs?
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')), ); }, ), ), ); } }
Look at what the Navigator's onGenerateRoute returns as the widget.
The Navigator widget creates a new navigation stack inside the Scaffold body. It builds a route that shows a centered text 'Nested Screen'. So the visible UI is that text centered on the screen.
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?
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')), ); }, ), ), ); } }
Think about how Flutter handles back button events when nested navigators cannot pop.
If the nested Navigator cannot pop any page, the back button event bubbles up to the outer Navigator or system. So the app will close or pop the outer route if any.
In a Flutter app with a nested Navigator inside a tab, what happens to the nested Navigator's stack when switching tabs?
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'), ], ), ), ); } }
Consider how IndexedStack keeps widgets alive in Flutter.
Using IndexedStack keeps the nested Navigators alive in memory, so their navigation stacks are preserved when switching tabs.
Which option correctly defines a nested Navigator with named routes in 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( // ??? ); } }
Check the Navigator constructor parameters for named routes and initialRoute.
Navigator accepts initialRoute and routes map to define named routes. Option B correctly uses both.
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?
Think about how Flutter identifies widgets and preserves their state.
Using a new GlobalKey each time rebuilds the Navigator widget, causing state loss. The key must be stable to preserve navigation stack.