Skip to content

Instantly share code, notes, and snippets.

@hectorAguero
Last active January 12, 2021 19:49
Show Gist options
  • Save hectorAguero/83bec6bbc8dd9965340170a06e8a0ea7 to your computer and use it in GitHub Desktop.
Save hectorAguero/83bec6bbc8dd9965340170a06e8a0ea7 to your computer and use it in GitHub Desktop.
Flutter: pass Arguments as a Route
//FILE main.dart
import 'package:flutter/material.dart';
//import 'routes.dart'
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Material App',
initialRoute: 'home',
routes: myRoutes,
onGenerateRoute: (settings) => generatedRoutes(settings));
}
}
class HomePage extends StatefulWidget {
const HomePage({Key key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
RaisedButton(
onPressed: () {
Navigator.pushNamed(context, 'otherPage',
arguments: OtherPageArguments('Hello World', 2));
},
child: const Text('Go to Page with Arguments'),
),
const SizedBox(height: 32),
RaisedButton(
onPressed: () {
Navigator.pushNamed(context, 'helpPage');
},
child: const Text('Go to Page without Arguments'),
),
],
),
),
);
}
}
///FILE routes.dart MOVE THE BELOW CODE TO ANOTHER FILE
//import 'pages.dart'
//When routes doesn't have arguments goes here,
Map<String, WidgetBuilder> get myRoutes => <String, WidgetBuilder>{
'home': (context) => const HomePage(),
'helpPage': (context) => const HelpPage(),
};
//When routes have arguments goes here, if the route has more than 1 argument, need to pass those inside 1 class as argument.
Route<dynamic> generatedRoutes(RouteSettings settings) {
switch (settings.name) {
case 'otherPage':
final args = settings.arguments as OtherPageArguments;
return MaterialPageRoute(
builder: (context) {
return OtherPage(
name: args.name,
quantity: args.quantity,
);
},
);
break;
default:
return null;
}
}
class OtherPageArguments {
String name;
int quantity;
OtherPageArguments(this.name, this.quantity);
}
//FILE pages.dart MOVE THE BELOW CODE TO ANOTHER FILE
///import 'routes.dart'
class HelpPage extends StatefulWidget {
const HelpPage({Key key}) : super(key: key);
@override
_HelpPageState createState() => _HelpPageState();
}
class _HelpPageState extends State<HelpPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('No arguments'),
),
body: Container());
}
}
class OtherPage extends StatefulWidget {
final String name;
final int quantity;
const OtherPage({@required this.name, @required this.quantity, Key key})
: super(key: key);
@override
_OtherPageState createState() => _OtherPageState();
}
class _OtherPageState extends State<OtherPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(widget.name)),
body: Center(
child: Text('${widget.name} & ${widget.quantity}'),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment