Why Use It
Compare to Normal Route using this route has additional advantage, if you need to navigate to the same screen in many parts of your app, this approach can result in code duplication. The solution is to define a named route, and use the named route for navigation.
import 'package:flutter/material.dart'; void main() { runApp(MaterialApp( title: 'Named Routes Demo', // Start the app with the "/" named route. In this case, the app starts // on the FirstScreen widget. //When using initialRoute, don’t define a home property. initialRoute: '/', routes: { // When navigating to the "/" route, build the FirstScreen widget. '/': (context) => FirstScreen(), // When navigating to the "/second" route, build the SecondScreen widget. '/second': (context) => SecondScreen(), }, )); } class FirstScreen extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('First Screen'), ), body: Center( child: RaisedButton( child: Text('Launch screen'), onPressed: () { // Navigate to the second screen using a named route. Navigator.pushNamed(context, '/second'); }, ), ), ); } } class SecondScreen extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("Second Screen"), ), body: Center( child: RaisedButton( onPressed: () { // Navigate back to the first screen by popping the current route // off the stack. Navigator.pop(context); }, child: Text('Go back!'), ), ), ); } }
There are 0 comments