管理多個頁面時有兩個核心概念和類:Route 和 Navigator。 一個Route是一個屏幕或頁面的抽象,Navigator是管理Route的Widget。Navigator能夠經過route入棧和出棧來實現頁面之間的跳轉。ios
Route 一個頁面要想被路由統一管理,必須包裝爲一個Route。 Route是一個抽象類,進入Route類,點擊cmd+opt+B查看其實現類。 bash
/**
* 使用 MaterialPageRoute
* Android: 從屏幕底部滑動到頂部
* iOS: 從屏幕右側滑動到左側
*/
Navigator.of(context).push(MaterialPageRoute(builder: (ctx) {
return FirstPage("a home message---first");
}));
/**
* 使用PageRouteBuilder漸變效果
*/
Navigator.of(context).push(PageRouteBuilder(pageBuilder: (ctx, anim1, anim2) {
return FadeTransition(
opacity: anim1, child: FirstPage("a home message----first"));
}));
複製代碼
Navigator:管理全部的Route的Widget,經過一個Stack來進行管理的。app
// 路由跳轉:傳入一個路由對象
Future<T> push<T extendsObject>(Route<T> route)
// 路由跳轉:傳入一個名稱(命名路由)
Future<T> pushNamed<T extendsObject>(
String routeName, {
Object arguments,
})
// 路由返回:能夠傳入一個參數
bool pop<T extendsObject>([ T result ])
複製代碼
Future result=Navigator.of(context).push(MaterialPageRoute(builder: (ctx) {
return FirstPage("first params");
}));
result.then((value){
print("pop 返回---$value");
});
複製代碼
import 'package:flutter/material.dart';
class FirstPage extends StatelessWidget {
static const routeName="/firstPage";
final String _message;
FirstPage(this._message);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("first page"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(_message, style: TextStyle(fontSize: 20),),
RaisedButton(
child: Text("first pop"),
onPressed: () {
Navigator.of(context).pop("first pop");
},
),
],
),
),
);
}
}
複製代碼
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter widget',
theme: ThemeData(
primarySwatch: Colors.blue,
),
routes: {
//命名路由
SecondPage.routeName:(context)=>SecondPage(),
ThirdPage.routeName: (context) => ThirdPage(),
},
//因爲FirstPage已經建立了構造器
onGenerateRoute: (settings){
if (settings.name == FirstPage.routeName) {
return MaterialPageRoute(
builder: (context) {
return FirstPage(settings.arguments);
}
);
}
return null;
},
//未知路由
onUnknownRoute: (settings){
return MaterialPageRoute(
builder: (context){
return UnKnownPage();
}
);
},
home: MyHomePage(),
);
}
}
複製代碼
Navigator.of(context).pushNamed(SecondPage.routeName, arguments: "a home message-03");
複製代碼
SecondPage代碼,裏面獲取參數less
import 'package:flutter/material.dart';
class SecondPage extends StatelessWidget {
static const routeName="/secondPage";
@override
Widget build(BuildContext context) {
final _message = ModalRoute.of(context).settings.arguments as String;
return Scaffold(
appBar: AppBar(
title: Text("second page"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(_message, style: TextStyle(fontSize: 20),),
RaisedButton(
child: Text("second pop"),
onPressed: () {
Navigator.of(context).pop("second pop");
},
),
],
),
),
);
}
}
複製代碼
appBar: AppBar(
title: Text("third page"),
leading: IconButton(
icon: Icon(Icons.arrow_back_ios),
onPressed: () {
Navigator.of(context).pop("third page back");
},
),
),
複製代碼
import 'package:flutter/material.dart';
class ThirdPage extends StatelessWidget {
static const routeName = "/thirdPage";
@override
Widget build(BuildContext context) {
final _message = ModalRoute.of(context).settings.arguments as String;
return WillPopScope(
onWillPop: () {
// 當返回爲true時,flutter自動幫助咱們執行返回操做
// 當返回爲false時, 自行寫返回代碼
Navigator.of(context).pop("third page back");
return Future.value(false);
},
child: Scaffold(
appBar: AppBar(
title: Text("third page"),
// leading: IconButton(
// icon: Icon(Icons.arrow_back_ios),
// onPressed: () {
// Navigator.of(context).pop("third pop");
// },
// ),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
_message,
style: TextStyle(fontSize: 20),
),
RaisedButton(
child: Text("third pop"),
onPressed: () {
Navigator.of(context).pop("third page back");
},
),
],
),
),
),
);
}
}
複製代碼