Arguments

The pushNamed method also accepts arguments, to pass to the new Route:

Navigator.of(context).pushNamed('/2', arguments: "Hello from screen 1");

In this case, we need to use onGenerateRoute from WidgetsApp so that we have access to these arguments through the RouteSettings object:

// navigation_widgetsapp_named_routes_arguments.dart
class _NavigatorNamedRoutesArgumentsAppState
extends State<NavigatorNamedRoutesArgumentsApp> {
@override
Widget build(BuildContext context) {
return WidgetsApp(
color: Colors.blue,
onGenerateRoute: (settings) {
if(settings.name == '/') {
return MaterialPageRoute(
builder: (context) => _screen1(context)
);
} else if(settings.name == '/2') {
return MaterialPageRoute(
builder: (context) => _screen2(context, settings.arguments)
);
}
},
);
}
...
}

After that, we use the argument normally found in in the _screen2 builder, to display an additional message.

When using the Routes creation on demand, it looks easier to pass arguments, as you will build the widget at the time you need and can customize the creation by passing arguments as you need.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.28.50