Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of the 'routes table' and the pushedNamed() function.
import 'package:flutter/material.dart';
void main() => runApp(NavigateNamedRoutes());
class NavigateNamedRoutes extends StatelessWidget {
@override
Widget build(BuildContext context) => MaterialApp(
title: 'Named Routes Demo',
initialRoute: '/',
routes: {
'/': (context) => FirstScreen(),
'/second': (context) => SecondScreen(),
},
onGenerateRoute: Router.generateRoute,
);
}
class FirstScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() => _FirstScreenState();
}
class _FirstScreenState extends State<FirstScreen> {
/// Displayed at the center of the screen.
dynamic data = '';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('First Screen'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
child: const Text('Second screen'),
onPressed: () async {
data = await Navigator.pushNamed(context, '/second');
setState(() {});
}),
ElevatedButton(
child: const Text('Third screen'),
onPressed: () async {
data = await Navigator.pushNamed(context, '/third',
arguments: 'argument!');
setState(() {});
}),
ElevatedButton(
child: const Text('Fourth screen'),
onPressed: () async {
data = await Navigator.pushNamed(context, '/fourth',
arguments: 'argument!');
setState(() {});
}),
Text(
"${data ?? ''}",
style: Theme.of(context).textTheme.headline4,
),
],
),
),
);
}
}
class SecondScreen extends StatelessWidget {
final title = 'Second Screen';
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Center(
child: ElevatedButton(
onPressed: () => Navigator.pop(context, title),
child: const Text('Go back!'),
),
),
);
}
class ThirdScreen extends StatelessWidget {
ThirdScreen({Key key, this.arguments}) : super(key: key);
final Object arguments;
final title = 'Third Screen';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Center(
child: ElevatedButton(
onPressed: () => Navigator.pop<String>(context, "$title ${arguments ?? ''}"),
child: const Text('Go back!'),
),
),
);
}
}
class FourthScreen extends StatelessWidget {
FourthScreen({Key key, this.arguments}) : super(key: key);
final Object arguments;
final title = 'Fourth Screen';
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Center(
child: ElevatedButton(
onPressed: () => Navigator.pop(context, "$title ${arguments ?? ''}"),
child: const Text('Go back!'),
),
),
);
}
class Router {
/// passed to the widget, MaterialApp
static Route<T> generateRoute<T>(RouteSettings settings) {
var func;
switch (settings.name) {
case '/third':
func = (_) => ThirdScreen(arguments: settings.arguments);
break;
case '/fourth':
func = (_) => FourthScreen(arguments: settings.arguments);
break;
default:
func = (_) => Scaffold(
body:
Center(child: Text('No route defined for ${settings.name}')),
);
}
return _pageRoute(func);
}
static _pageRoute(WidgetBuilder builder) =>
MaterialPageRoute(builder: builder);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment