设置routes, 单独设置一个routes.dart文件
// routes.dart
Map<String, Widget Function(BuildContext)> routes = {
"/login": (context) => loginController(),
'/main': (context) => MainTabBarView(),
'/verifyInfo': (context) => VerifyInfoController(), // 我的页面- 认证信息
'/pwdManager': (context) => PwdManagerController(), // 我的页面- 密码管理
'/webviewLoadHTTP': (context) => WebviewLoadHTTP(), // webView加载HTTP URL
'/webviewLoadHTML': (context) => WebviewLoadHTML(), // webView加载HTML
'/checkUpdate': (context) => CheckUpdateController(), // 我的页面- 检查版本更新
};
在main.dart中
// main.dart
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primaryColor: Global.theme,
brightness: Brightness.light,
),
debugShowCheckedModeBanner: false,
routes: routes, // 设置routes
home: loginController(),
);
}
}
优点:页面路由只在routes.dart中设置,方便查看。
缺点:这种方式往下一个页面传值,需要在下一个页面加载时才能获取到这个值。如果下个页面setState()时,值也会刷新一下,如果传递的时id,通过id在网络获取详情信息时,刷新页面时也会重新请求数据,请求完再刷新,就会造成死循环。