Created
July 25, 2020 23:47
-
-
Save Nailik/13c8bd950611310ee9a05ceee80f3940 to your computer and use it in GitHub Desktop.
named routes arguments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
// Provide a function to handle named routes. Use this function to | |
// identify the named route being pushed, and create the correct | |
// Screen. | |
onGenerateRoute: (settings) { | |
// If you push the PassArguments route | |
if (settings.name == PassArgumentsScreen.routeName) { | |
// Cast the arguments to the correct type: ScreenArguments. | |
final ScreenArguments args = settings.arguments; | |
// Then, extract the required data from the arguments and | |
// pass the data to the correct screen. | |
return MaterialPageRoute( | |
builder: (context) { | |
return PassArgumentsScreen( | |
title: args.title, | |
message: args.message, | |
); | |
}, | |
); | |
} | |
// The code only supports PassArgumentsScreen.routeName right now. | |
// Other values need to be implemented if we add them. The assertion | |
// here will help remind us of that higher up in the call stack, since | |
// this assertion would otherwise fire somewhere in the framework. | |
assert(false, 'Need to implement ${settings.name}'); | |
return null; | |
}, | |
title: 'Navigation with Arguments', | |
home: HomeScreen(), | |
routes: { | |
ExtractArgumentsScreen.routeName: (context) => | |
ExtractArgumentsScreen(), | |
}); | |
} | |
} | |
class HomeScreen extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Home Screen'), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
// A button that navigates to a named route that. The named route | |
// extracts the arguments by itself. | |
RaisedButton( | |
child: Text("Navigate to screen that extracts arguments"), | |
onPressed: () { | |
// When the user taps the button, navigate to a named route | |
// and provide the arguments as an optional parameter. | |
Navigator.pushNamed( | |
context, | |
ExtractArgumentsScreen.routeName, | |
arguments: ScreenArguments( | |
'Extract Arguments Screen', | |
'This message is extracted in the build method.', | |
), | |
); | |
}, | |
), | |
// A button that navigates to a named route. For this route, extract | |
// the arguments in the onGenerateRoute function and pass them | |
// to the screen. | |
RaisedButton( | |
child: Text("Navigate to a named that accepts arguments"), | |
onPressed: () { | |
// When the user taps the button, navigate to a named route | |
// and provide the arguments as an optional parameter. | |
Navigator.pushNamed( | |
context, | |
PassArgumentsScreen.routeName, | |
arguments: ScreenArguments( | |
'Accept Arguments Screen', | |
'This message is extracted in the onGenerateRoute function.', | |
), | |
); | |
}, | |
), | |
], | |
), | |
), | |
); | |
} | |
} | |
// A Widget that extracts the necessary arguments from the ModalRoute. | |
class ExtractArgumentsScreen extends StatelessWidget { | |
static const routeName = '/extractArguments'; | |
@override | |
Widget build(BuildContext context) { | |
// Extract the arguments from the current ModalRoute settings and cast | |
// them as ScreenArguments. | |
final ScreenArguments args = ModalRoute.of(context).settings.arguments; | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(args.title), | |
), | |
body: Center( | |
child: Text(args.message), | |
), | |
); | |
} | |
} | |
// A Widget that accepts the necessary arguments via the constructor. | |
class PassArgumentsScreen extends StatelessWidget { | |
static const routeName = '/passArguments'; | |
final String title; | |
final String message; | |
// This Widget accepts the arguments as constructor parameters. It does not | |
// extract the arguments from the ModalRoute. | |
// | |
// The arguments are extracted by the onGenerateRoute function provided to the | |
// MaterialApp widget. | |
const PassArgumentsScreen({ | |
Key key, | |
@required this.title, | |
@required this.message, | |
}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(title), | |
), | |
body: Center( | |
child: Text(message), | |
), | |
); | |
} | |
} | |
// You can pass any object to the arguments parameter. In this example, | |
// create a class that contains both a customizable title and message. | |
class ScreenArguments { | |
final String title; | |
final String message; | |
ScreenArguments(this.title, this.message); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment