Skip to content

Instantly share code, notes, and snippets.

@ltvu93
Created May 20, 2020 11:00
Show Gist options
  • Save ltvu93/823113166b8e8dc9506758989220c0e5 to your computer and use it in GitHub Desktop.
Save ltvu93/823113166b8e8dc9506758989220c0e5 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: HomeScreen.routeName,
onGenerateRoute: AppRoutes.getRoute,
);
}
}
class AppRoutes {
AppRoutes._();
static Route getRoute(RouteSettings settings) {
print('test test ${settings.name}');
switch (settings.name) {
case HomeScreen.routeName:
return _buildRoute(settings, HomeScreen());
case NormalScreen.routeName:
return _buildRoute(settings, new NormalScreen());
case ArgumentsScreen.routeName:
final argumentValue = settings.arguments;
return _buildRoute(settings, new ArgumentsScreen(argumentValue));
default:
return throw Exception("Route name is incorrect.");
}
}
static MaterialPageRoute _buildRoute(RouteSettings settings, Widget builder) {
return new MaterialPageRoute(
settings: settings,
builder: (BuildContext context) => builder,
);
}
}
class HomeScreen extends StatelessWidget {
static const routeName = '/';
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
RaisedButton(
onPressed: () {
Navigator.pushNamed(
context,
NormalScreen.routeName,
);
},
child: Text('Go to Normal screen'),
),
SizedBox(height: 16.0),
RaisedButton(
onPressed: () {
Navigator.pushNamed(
context,
ArgumentsScreen.routeName,
arguments: 'Hello world',
);
},
child: Text('Go to Argument screen'),
),
],
),
),
),
);
}
}
class NormalScreen extends StatelessWidget {
static const routeName = '/normal';
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Center(
child: Text('Normal screen'),
),
),
);
}
}
class ArgumentsScreen extends StatelessWidget {
static const routeName = '/arguments';
final String value;
ArgumentsScreen(this.value);
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text('Normal screen'),
SizedBox(height: 16.0),
Text('Argument value: $value'),
],
),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment