Johnhong9527 5 years ago
parent
commit
f3ab410011
1 changed files with 48 additions and 30 deletions
  1. 48 30
      app/lib/main.dart

+ 48 - 30
app/lib/main.dart

@@ -9,25 +9,37 @@ class MyApp extends StatelessWidget {
   @override
   Widget build(BuildContext context) {
     return MaterialApp(
-      title: 'Flutter Demo',
-      theme: ThemeData(
-        // This is the theme of your application.
-        //
-        // Try running your application with "flutter run". You'll see the
-        // application has a blue toolbar. Then, without quitting the app, try
-        // changing the primarySwatch below to Colors.green and then invoke
-        // "hot reload" (press "r" in the console where you ran "flutter run",
-        // or simply save your changes to "hot reload" in a Flutter IDE).
-        // Notice that the counter didn't reset back to zero; the application
-        // is not restarted.
-        primarySwatch: Colors.blueGrey,
-        // This makes the visual density adapt to the platform that you run
-        // the app on. For desktop platforms, the controls will be smaller and
-        // closer together (more dense) than on mobile platforms.
-        visualDensity: VisualDensity.adaptivePlatformDensity,
-      ),
-      home: MyHomePage(title: 'Flutter Demo Home Page'),
-    );
+        title: 'Flutter Demo',
+        theme: ThemeData(
+          // This is the theme of your application.
+          //
+          // Try running your application with "flutter run". You'll see the
+          // application has a blue toolbar. Then, without quitting the app, try
+          // changing the primarySwatch below to Colors.green and then invoke
+          // "hot reload" (press "r" in the console where you ran "flutter run",
+          // or simply save your changes to "hot reload" in a Flutter IDE).
+          // Notice that the counter didn't reset back to zero; the application
+          // is not restarted.
+          primarySwatch: Colors.blue,
+          // This makes the visual density adapt to the platform that you run
+          // the app on. For desktop platforms, the controls will be smaller and
+          // closer together (more dense) than on mobile platforms.
+          visualDensity: VisualDensity.adaptivePlatformDensity,
+        ),
+        // home: MyHomePage(title: 'Flutter Demo Home Page'),
+        routes: {
+          "new_page": (context) => NewRoute(),
+          "/": (context) => MyHomePage(title: '首页'), //注册首页路由
+        },
+        onGenerateRoute: (RouteSettings settings) {
+          return MaterialPageRoute(builder: (context) {
+            String routeName = settings.name;
+            print(routeName);
+            // 如果访问的路由页需要登录,但当前未登录,则直接返回登录页路由,
+            // 引导用户登录;其它情况则正常打开路由。
+          });
+        });
+    // 路由钩子
   }
 }
 
@@ -106,17 +118,21 @@ class _MyHomePageState extends State<MyHomePage> {
             ),
             FlatButton(
               child: Text("open new route"),
-              textColor: Colors.blue,
+              textColor: Colors.red,
               onPressed: () {
+                // 普通路由导航
+                // Navigator.pushNamed(context, "new_page");
+                // 在打开路由时传递参数
+                Navigator.of(context).pushNamed("new_page", arguments: "hi");
                 //导航到新路由
-                Navigator.push(
-                    context,
-                    MaterialPageRoute(
-                        builder: (context) {
-                          return NewRoute();
-                        },
-                        maintainState: false,
-                        fullscreenDialog: false));
+                // Navigator.push(
+                //     context,
+                //     MaterialPageRoute(
+                //         builder: (context) {
+                //           return NewRoute();
+                //         },
+                //         maintainState: false,
+                //         fullscreenDialog: false));
               },
             ),
             RouterTestRoute()
@@ -136,12 +152,14 @@ class _MyHomePageState extends State<MyHomePage> {
 class NewRoute extends StatelessWidget {
   @override
   Widget build(BuildContext context) {
+    //获取路由参数
+    var args = ModalRoute.of(context).settings.arguments;
     return Scaffold(
       appBar: AppBar(
-        title: Text("New route"),
+        title: Text("New route" + args),
       ),
       body: Center(
-        child: Text("This is new route"),
+        child: Text("This is new route" + args),
       ),
     );
   }