基本Route跳轉(zhuǎn):
跳轉(zhuǎn):
Navigator.push(
? ? ? ? ? ? ? context,
? ? ? ? ? ? ? ? ? MaterialPageRoute(
? ? ? ? ? ? ? ? ? ? ? builder: (context) => second.Parent(),
? ? ? ? ? ? ? ? ? ? ? settings: RouteSettings(name: '/Second', arguments: 'This is arguments')));
其中RouteSettings的name屬性介紹
? /// The name of the route (e.g., "/settings").
? /// If null, the route is anonymous.
彈出:
Navigator.pop(context);
命名Route跳轉(zhuǎn):
定義routes信息:
MaterialApp(
? ? ? title: 'Flutter Demo',
? ? ? theme: ThemeData(
? ? ? ? primarySwatch: Colors.blue,
? ? ? ),
? ? ? initialRoute: '/',
? ? ? routes: {
? ? ? ? '/': (context) => FirstPage(),
? ? ? ? '/second': (context) => SecondPage()
? ? ? },
? ? );
跳轉(zhuǎn):
Navigator.pushNamed(context, '/Second',arguments: 'This is arguments');
//這里'/Second'是routeName悍抑,最終賦值給RouteSettings的name屬性
彈出:
Navigator.pop(context);
檢索route的順序:
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.
MaterialApp中initialRoute屬性:
官方介紹:
? /// Defaults to [Window.defaultRouteName], which may be overridden by the code
? /// that launched the application.
? ///
? /// If the route contains slashes, then it is treated as a "deep link", and
? /// before this route is pushed, the routes leading to this one are pushed
? /// also. For example, if the route was `/a/b/c`, then the app would start
? /// with the three routes `/a`, `/a/b`, and `/a/b/c` loaded, in that order.
? ///
? /// If any part of this process fails to generate routes, then the
? /// [initialRoute] is ignored and [Navigator.defaultRouteName] is used instead
? /// (`/`). This can happen if the app is started with an intent that specifies
? /// a non-existent route.
? /// The [Navigator] is only built if routes are provided (either via [home],
? /// [routes], [onGenerateRoute], or [onUnknownRoute]); if they are not,
? /// [initialRoute] must be null and [builder] must not be null.
但經(jīng)實(shí)際測試割去,對于initialRoute:'/a/b/c', 系統(tǒng)也會(huì)檢測`/`,即
I/flutter (29676): * /
I/flutter (29676):? * /a
I/flutter (29676):? * /a/b
I/flutter (29676):? * /a/b/c
這里我很納悶醉拓,不知道是不是官方文檔的問題,歡迎交流討論套鹅。
運(yùn)行后系統(tǒng)會(huì)逐個(gè)加載四個(gè)route name對應(yīng)的widget頁面轧拄,點(diǎn)返回鍵會(huì)依次返回挥下。
一旦one or more of those objects was null, and therefore the initial route specified will be ignored and?"/" will be used instead.
錯(cuò)誤示例:
1. 如果賦值形如initialRoute:'/a'揍魂,會(huì)報(bào)異常但不會(huì)crash,會(huì)提示'/'和'/a'不能在route表中全被找到就會(huì)被默認(rèn)置為'/':
? ? ? initialRoute: '/a',
? ? ? routes: {
? ? ? ? '/': (context) => FirstPage(),
? ? ? ? '/b': (context) => SecondPage()
? ? ? }
I/flutter (10022): ══╡ EXCEPTION CAUGHT BY FLUTTER FRAMEWORK ╞═════════════════════════════════════════════════════════
I/flutter (10022): The following message was thrown:
I/flutter (10022): Could not navigate to initial route.
I/flutter (10022): The requested route name was: "/a"
I/flutter (10022): The following routes were therefore attempted:
I/flutter (10022):? * /
I/flutter (10022):? * /a
I/flutter (10022): This resulted in the following objects:
I/flutter (10022):? * MaterialPageRoute(RouteSettings("/", null), animation: null)
I/flutter (10022):? * null
I/flutter (10022): One or more of those objects was null, and therefore the initial route specified will be ignored and
I/flutter (10022): "/" will be used instead.
2. 如果'/'不在routes中指定棚瘟,且沒有home屬性现斋,就會(huì)crash
? ? ? initialRoute: '/a',
? ? ? routes: {
? ? ? ? '/a': (context) => FirstPage(),
? ? ? ? '/b': (context) => SecondPage()
? ? ? }
I/flutter (10022): ══╡ EXCEPTION CAUGHT BY FLUTTER FRAMEWORK ╞═════════════════════════════════════════════════════════
I/flutter (10022): The following message was thrown:
I/flutter (10022): Could not navigate to initial route.
I/flutter (10022): The requested route name was: "/a"
I/flutter (10022): The following routes were therefore attempted:
I/flutter (10022):? * /
I/flutter (10022):? * /a
I/flutter (10022): This resulted in the following objects:
I/flutter (10022):? * null
I/flutter (10022):? * MaterialPageRoute(RouteSettings("/a", null), animation: null)
I/flutter (10022): One or more of those objects was null, and therefore the initial route specified will be ignored and
I/flutter (10022): "/" will be used instead.
I/flutter (10022): ════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter (10022): Another exception was thrown: Could not find a generator for route RouteSettings("/", null) in the _WidgetsAppState.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
3. 注意routes:屬性里的'/'不能和home屬性同時(shí)出現(xiàn):
? ? ? routes: {
? ? ? ? '/': (context) => FirstPage(),
? ? ? ? '/b': (context) => SecondPage()
? ? ? }
? ? home: FirstPage()
I/flutter (10022): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (10022): The following assertion was thrown building MaterialApp(dirty, state: _MaterialAppState#0574c):
I/flutter (10022): If the home property is specified, the routes table cannot include an entry for "/", since it would
I/flutter (10022): be redundant.
I/flutter (10022): 'package:flutter/src/widgets/app.dart':
I/flutter (10022): Failed assertion: line 169 pos 10: 'home == null ||
I/flutter (10022):? ? ? ? ? !routes.containsKey(Navigator.defaultRouteName)'