本文中涉及部分非Flutter的stable
版本(2019年2月25日 爲止)
,您可查閱Flutter相關文檔查看新功能是否同步的stable
分支或者切換其餘分支(如master
)中體驗git
在咱們使用Flutter開發App時,路由是必須用到的。github
Flutter提供了兩種方式進行路由,分別是 Navigator.push()
(例) 以及 Navigator.pushNamed()
(例)。markdown
對於這兩種方式各有各的優勢與缺點:app
在實際開發運用中,咱們也是更加傾向於Named
路由方式。框架
可是惟一不足之處就是它不支持傳遞參數,這一點實在是使人抓狂。less
其實在Flutter的Issues中16年就有人提出這個問題,可是這個Issue一直到今年纔有PR被提交(目前爲止這個功能僅僅被合併進master分支並無同步到bate或者stable)
ide
執行
flutter channel master
而後再運行命令flutter upgrade
保證你處於最新的版本。便可切換到master分支下優化
main.dart
中略做修改import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatelessWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(this.title),
),
body: Center(
child: GestureDetector(
onTap: (){
// TODO
},
child: Text("go next page with params"),
),
),
);
}
}
複製代碼
page.dart
import 'package:flutter/material.dart';
class Page extends StatelessWidget{
@override
Widget build(BuildContext context) {
return Material(
child: Center(
child: Text("hi this is next page"),
),
);
}
}
複製代碼
main.dart
import 'package:flutter/material.dart';
// 引入新頁面
import 'page.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
// 處理Named頁面跳轉 傳遞參數
onGenerateRoute: (RouteSettings setting) {
if(setting.name == '/page') {
return MaterialPageRoute(builder: (context) => Page(id: setting.arguments['id']));
}
},
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatelessWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(this.title),
),
body: Center(
child: GestureDetector(
onTap: (){
// 進行Named頁面跳轉 傳遞參數
Navigator.pushNamed(context, '/page', arguments: { "id": 1} );
},
child: Text("go next page with params"),
),
),
);
}
}
複製代碼
page.dart
import 'package:flutter/material.dart';
class Page extends StatelessWidget{
Page({this.id});
final int id;
@override
Widget build(BuildContext context) {
return Material(
child: Center(
child: Text("hi this is next page, id is $id"),
),
);
}
}
複製代碼
這裏咱們仍是要在onGenerateRoute
處理咱們的Named跳轉,咱們在對他進行優化一下。動畫
(目前沒辦法在MaterialApp的routes進行處理,緣由可見源碼(一)(二))ui
源碼一處:調用
widget.pageRouteBuilder
並傳入settings
(arguments
在settings
中)源碼二處:
pageRouteBuilder
被寫死爲構建一個MaterialPageRoute
,而MaterialPageRoute
中並無對settings
進行傳遞因此咱們只能本身在
onGenerateRoute
中進行處理。(但願官網也能繼續優化,emmmm拭目以待)
/** * main.dart */
import 'package:flutter/material.dart';
// 引入新頁面
import 'page.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// 定義路由信息
final Map<String, Function> routes = {
'/page': (context, {arguments}) => Page(arguments: arguments)
};
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
// 處理Named頁面跳轉 傳遞參數
onGenerateRoute: (RouteSettings settings) {
// 統一處理
final String name = settings.name;
final Function pageContentBuilder = this.routes[name];
if (pageContentBuilder != null) {
final Route route = MaterialPageRoute(
builder: (context) =>
pageContentBuilder(context, arguments: settings.arguments));
return route;
}
},
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatelessWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(this.title),
),
body: Center(
child: GestureDetector(
onTap: () {
// 進行Named頁面跳轉 傳遞參數
Navigator.pushNamed(context, '/page', arguments: {'id': 123});
},
child: Text("go next page with params"),
),
),
);
}
}
/** * page.dart */
import 'package:flutter/material.dart';
class Page extends StatelessWidget{
Page({this.arguments});
final Map arguments;
@override
Widget build(BuildContext context) {
return Material(
child: Center(
child: Text("hi this is next page, id is ${arguments != null ? arguments['id'] : '0'}"),
),
);
}
}
複製代碼