1、定义路由,在MaterialApp下
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Demo',
initialRoute: "/",
routes: {
// "/": (context) => AnimationPage(),
"/": (context) => AppFrame(),
"/login": (context) => LoginPage(),
"/self_detail": (context) => SelfDetail(),
},
);
}
当然也可以通过onGenerateRoute方式来,但是就不能用routes
2、定义要传递的参数类
class SelfDetailArg {
final String title;
SelfDetailArg(this.title);
}
3、路由跳转并传递参数
onPressed: () {
Navigator.of(context).pushNamed("/self_detail", arguments: SelfDetailArg('版本号: v1.0.0'));
},
4、在路由页面获取传递过来的参数
final SelfDetailArg arg = ModalRoute.of(context)?.settings.arguments as SelfDetailArg;
5、遇到的问题: 官方已经说了4种可能,第五种可能是存在2个MaterialApp
Could not find a generator for route RouteSettings("/HomePage", null) in the _WidgetsAppState.
Generators for routes are searched for in the following order:
1. For the "/" route, the "home" property, if non-null, is used.
2. Otherwise, the "routes" table is used, if it has an entry for the route.
3. Otherwise, onGenerateRoute is called. It should return a non-null value for any valid route not handled by "home" and "routes".
4. Finally if all else fails onUnknownRoute is called.
Unfortunately, onUnknownRoute was not set.