路由(Route)在移动开发中通常指页面(Page),这跟web开发中单页应用的Route概念意义是相同的,Route在Android中通常指一个Activity,在iOS中指一个ViewController。所谓路由管理,就是管理页面之间如何跳转,通常也可被称为导航管理。这和原生开发类似,无论是Android还是iOS,导航管理都会维护一个路由栈,路由入栈(push)操作对应打开一个新页面,路由出栈(pop)操作对应页面关闭操作,而路由管理主要是指如何来管理路由栈。
文件: main.dart
// 此行代码作用是导入了Material UI组件库。Material是一种标准的移动端和web端的视觉设计语言,
// Flutter默认提供了一套丰富的Material风格的UI组件。
import 'package:flutter/material.dart';
import 'package:flutter_app/demo/NewRoute.dart';
import 'package:flutter_app/demo/OtherNewRoute.dart';
// runApp 方法,它的功能是启动Flutter应用,它接受一个Widget参数.
void main() => runApp(MyApp());
// MyApp 是一个 Flutter 应用。
class MyApp extends StatelessWidget {
// This widget is the root of your application.
// 提供一个build()方法来描述如何构建UI界面(通常是通过组合、拼装其它基础widget)。
@override
Widget build(BuildContext context) {
// 是Material库中提供的Flutter APP框架,通过它可以设置应用的名称、主题、语言、首页及路由列表等。
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.blue,
),
//注册路由表
routes:{
"new_page":(context)=>NewRoute(),
} ,
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
// MyHomePage 是应用的首页,它继承自StatefulWidget类,表示它是一个有状态的widget(Stateful widget)
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
// 构建UI界面,为什么build()方法在State(而不是StatefulWidget)中 ?这主要是为了开发的灵活性。
// 如果将build()方法在StatefulWidget中则会有两个问题:见:https://book.flutterchina.club/chapter2/first_flutter_app.html
// (1)状态访问不便
// (2)继承StatefulWidget不便,
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
// Scaffold 是Material库中提供的页面脚手架,它包含导航栏和Body以及FloatingActionButton。
// 它提供了默认的导航栏、标题和包含主屏幕widget树的body属性。
// 本书后面示例中,路由默认都是通过Scaffold创建。
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
// body的widget树中包含了一个Center widget,Center 可以将其子widget树对齐到屏幕中心,
// Center 子widget是一个Column widget,Column的作用是将其所有子widget沿屏幕垂直方向依次排列,
// 此例中Column包含两个 Text子widget,第一个Text widget显示固定文本 “You have pushed the button this many times:”,
// 第二个Text widget显示_counter状态的数值
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
FlatButton(
child: Text("open new route"),
textColor: Colors.blue,
onPressed: () {
Navigator.pushNamed(context, "new_page");
},
),
FlatButton(
child: Text("open Other new route"),
textColor: Colors.blue,
onPressed: () {
//导航到新路由
Navigator.push( context,
// MaterialPageRoute 是Material组件库的一个Widget,它可以针对不同平台,
// 实现与平台页面切换动画风格一致的路由切换动画
new MaterialPageRoute(builder: (context) {
return new OtherNewRoute();
}));
},
),
],
),
),
);
}
}
文件:NewRoute.dart
import 'package:flutter/material.dart';
class NewRoute extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("New route"),
),
body: Center(
child: Text("This is new route"),
),
);
}
}
MaterialPageRoute
继承自PageRoute
类,PageRoute
类是一个抽象类,表示占有整个屏幕空间的一个模态路由页面,它还定义了路由构建及切换时过渡动画的相关接口及属性。MaterialPageRoute
是Material组件库的一个Widget,它可以针对不同平台,实现与平台页面切换动画风格一致的路由切换动画:
下面我们介绍一下MaterialPageRoute
构造函数的各个参数的意义:
MaterialPageRoute({
WidgetBuilder builder,
RouteSettings settings,
bool maintainState = true,
bool fullscreenDialog = false,
})
builder
是一个WidgetBuilder类型的回调函数,它的作用是构建路由页面的具体内容,返回值是一个widget。我们通常要实现此回调,返回新路由的实例。settings
包含路由的配置信息,如路由名称、是否初始路由(首页)。maintainState
:默认情况下,当入栈一个新路由时,原来的路由仍然会被保存在内存中,如果想在路由没用的时候释放其所占用的所有资源,可以设置maintainState
为false。fullscreenDialog
表示新的路由页面是否是一个全屏的模态对话框,在iOS中,如果fullscreenDialog
为true
,新页面将会从屏幕底部滑入(而不是水平方向)。Navigator
是一个路由管理的widget,它通过一个栈来管理一个路由widget集合。通常当前屏幕显示的页面就是栈顶的路由。Navigator
提供了一系列方法来管理路由栈,在此我们只介绍其最常用的两个方法:
将给定的路由入栈(即打开新的页面),返回值是一个Future
对象,用以接收新路由出栈(即关闭)时的返回数据。
将栈顶路由出栈,result
为页面关闭时返回给上一个页面的数据。
Navigator
还有很多其它方法,如Navigator.replace
、Navigator.popUntil
等,详情请参考API文档或SDK源码注释,在此不再赘述。下面我们还需要介绍一下路由相关的另一个概念“命名路由”。