Sub topic 8
Home Screen and Named Routes
This chapter assumes you know basic Dart syntax, can run a Flutter project, and understand StatelessWidget and the build method. You will use named routes to connect a home screen to a settings screen.
The home screen and route table
A route is a screen entry in Flutter's navigation stack. The navigation stack records which screens the user has visited. When a new screen is pushed, it goes on top of the current screen. When it is popped, Flutter returns to the screen below it.
MaterialApp can map route names to screen builders:
initialRouteselects the first route.routesmaps strings such as'/'and'/settings'to widgets.Navigator.pushNamedadds a named route to the stack.Navigator.popremoves the current route.
The / route conventionally represents the home screen.
| Configuration | Purpose |
|---|---|
| `initialRoute: '/'` | Starts the app at the home route |
| `routes: {...}` | Registers named screens |
| `Navigator.pushNamed(context, '/settings')` | Opens the settings screen |
| `Navigator.pop(context)` | Returns to the previous screen |
A route entry contains a function. Flutter calls that function when it needs to build the screen. The function receives a BuildContext, which allows it to access navigation and inherited app settings.
A runnable named-route example
Create this as lib/main.dart in a Flutter project:
import 'package:flutter/material.dart';
void main() {
runApp(const InboxApp());
}
class InboxApp extends StatelessWidget {
const InboxApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Inbox',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.indigo),
useMaterial3: true,
),
initialRoute: '/',
routes: {
'/': (context) => const HomePage(),
'/settings': (context) => const SettingsPage(),
},
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Inbox'),
actions: [
IconButton(
icon: const Icon(Icons.settings),
tooltip: 'Open settings',
onPressed: () {
Navigator.pushNamed(context, '/settings');
},
),
],
),
body: const Center(
child: Text('Your messages appear here.'),
),
);
}
}
class SettingsPage extends StatelessWidget {
const SettingsPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Settings'),
),
body: const Center(
child: Text('Change your preferences here.'),
),
);
}
}When the app starts, MaterialApp builds HomePage for /. Tapping the settings button calls pushNamed. Flutter looks up '/settings', builds SettingsPage, and places it above the home route.
Because the settings route was pushed on top of another route, its AppBar normally displays a back button automatically. Pressing that button pops the settings route. You can also return in code:
Navigator.pop(context);Use pop when a button completes an action, such as closing a form or canceling a dialog.
How navigation changes the widget tree
The route stack and widget tree work together. The app root stays in place while the active route changes below it.
Common errors
Registering a route incorrectly
The route map must contain a function that returns a widget. This is correct:
routes: {
'/settings': (context) => const SettingsPage(),
}This is incorrect because it supplies a widget instead of a route builder:
routes: {
'/settings': const SettingsPage(),
}Using a name that is not registered
Navigator.pushNamed(context, '/profile') fails if '/profile' is absent from the route table. Register every named route before navigating to it.
Pushing the home route repeatedly
Calling pushNamed(context, '/') from many screens creates another home route each time. Pressing Back then requires the user to pass through duplicate home screens. Use popUntil or a replacement method when you need to clear earlier routes.
Expecting data from a route automatically
Named routes do not automatically transfer screen data. For simple data, use arguments with pushNamed and read it from ModalRoute. For larger applications, a router package or an explicit state-management design may make data flow clearer.
Exercise
What happens when Navigator.pushNamed(context, '/settings') runs, and why does the settings screen show a back button?
Show answer
Flutter finds '/settings' in the MaterialApp route table, builds SettingsPage, and pushes it above the current route. The AppBar detects that the route can return to a previous route, so it provides a back button.