未改造代码
import 'package:flutter/material.dart';
import 'navigator_demo.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: LoginPage(),
);
}
}
import 'package:flutter/material.dart';
class LoginPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("登录"),
),
body: ElevatedButton(
onPressed: () async {
var result = await Navigator.of(context).push(MaterialPageRoute(
builder: (context) {
return MenuPage(
title: "导航菜单",
);
},
settings: RouteSettings(
name: "menu",
arguments: "",
),
maintainState: false,
fullscreenDialog: false
));
print(result);
},
child: Text("登录按钮")
),
);
}
}
class MenuPage extends StatelessWidget {
final String title;
const MenuPage({Key key, @required this.title}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: ElevatedButton(
onPressed: () {
Navigator.of(context).pop({
"name": "因为参数是Object,所以可以返回个对象"
});
},
child: Text("返回按钮"),
),
);
}
}
第一遍改造:参数传递发生变化
import 'package:flutter/material.dart';
class LoginPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("登录"),
),
body: ElevatedButton(
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) {
return MenuPage();
},
settings: RouteSettings(
name: "menu",
arguments: {"name", "张三"},
),
maintainState: false,
fullscreenDialog: false
)).then((value) => print(value));
},
child: Text("登录按钮")
),
);
}
}
class MenuPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
dynamic args = ModalRoute.of(context).settings.arguments;
return Scaffold(
appBar: AppBar(
title: Text("titile" + " " + args.toString()),
),
body: ElevatedButton(
onPressed: () {
Navigator.of(context).pop({
"name": "因为参数是Object,所以可以返回个对象"
});
},
child: Text("返回按钮"),
),
);
}
}
第二次改造:路由表简化
import 'package:flutter/material.dart';
import 'navigator_demo.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
routes: {
"/": (context) => LoginPage(),
"menu": (context) => MenuPage(),
},
);
}
}
import 'package:flutter/material.dart';
class LoginPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("登录"),
),
body: ElevatedButton(
onPressed: () {
Navigator.of(context)
.pushNamed("menu", arguments: "菜单导航")
.then((value) => print(value));
},
child: Text("登录按钮")
),
);
}
}
class MenuPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
dynamic args = ModalRoute.of(context).settings.arguments;
return Scaffold(
appBar: AppBar(
title: Text("titile" + " " + args.toString()),
),
body: ElevatedButton(
onPressed: () {
Navigator.of(context).pop({
"name": "因为参数是Object,所以可以返回个对象"
});
},
child: Text("返回按钮"),
),
);
}
}
第三次改造:路由守卫
import 'package:flutter/material.dart';
import 'navigator_demo.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
routes: {
"/": (context) => LoginPage(),
},
onGenerateRoute: (s) {
switch (s.name) {
case "menu":
return MaterialPageRoute(builder: (context) {
return MenuPage();
},
settings: s);
break;
default:
}
},
);
}
}