Navigator 路由原理
我们先来看看一个普通 App 路由会有什么样结构:
RenderObjectToWidgetAdapter: Flutter 中的 root widget.
MyApp: 我们在
void runApp(Widget app)中传入的自定义 Widget。MaterialApp:
MaterialApp组件Navigator:一个
statefulWidget他的作用就是用pop和push方法管理 route 集合和当前显示的 route。说白了就是个有 history 数据的集合Overlay:一层层向上覆盖 Widget 的组件使得新的组件覆盖在已有的组件上面, 它里面有个
List<OverlayEntity>合集。- OverlayEnvity: 一个 Dart 纯类包囊了我们的
MyPagewidget,它的作用是在看这个widget 是否需要绘制,和保持它的状态,为什么要用纯类不直接操作 widget?这就是聪明的地方如果 MyPage2 不需被创建 我们是否不应该将它加入到 Element 树 里面?。以下是这个类有的属性:- builder: widget build 引用。
- opaque:是否全屏不透明,如果是
true那么下面的一层就不需要绘制了。 - maintainState:这个 widget 我们要不要它活着,打个比方 我们的 MyPage1 做了一些操作去到了 MyPage2 上面,而且完全的覆盖了 MyPage1 ,那么问题来了,如果现在 MyPage1 不绘制我们需不需要维持它的状态?这就是这个属性的作用!
- OverlayEnvity: 一个 Dart 纯类包囊了我们的
_Theatre: 里面有个 theatre (剧院),剧院里有 onstage(舞台)和 offstage(观众席),舞台上是只在被显示的组件。观众席上是那些没被显示但
mantainState = true的组件。- onstage(舞台)里面有个 stack widget 它是用来处理 widget 的堆叠。

Overlay 和 Theatre 关系
来一个比方:我们定义了 Dialog(弹窗),page3, page2 和 page1,我们打开App 进入了 page3 然后在出现一个 Dialog 弹窗,这时候我们 App 的状态应该是这样的,
- Overlay 定义了 dialog, page3,page2 和 page1 的OverlayEntity
- 我们歌剧院里有dialog, page3,page2 和 page1
- 舞台上有dialog, page3,因为弹窗是透明的所以 page3 需要被绘制
- 观众席上有 page2 和 page1,因为它们被page 3 挡住了不需要被绘画所以我们只
build它们。

用法
一个简单的例子:
如果你看 MaterialPageRoute MaterialPageRoute继承自PageRoute类,PageRoute是一个抽象类,表示占有整个屏幕空间的一个模态路由页面,它还定义了路由构建及切换时过渡动画的相关接口及属性。MaterialPageRoute 是Material组件库提供的组件,它可以针对不同平台,实现与平台页面切换动画风格一致的路由切换动画:
import 'package:flutter/material.dart';void main() => runApp(new MyApp());class MyApp extends StatelessWidget {@overrideWidget build(BuildContext context) {return new MaterialApp(title: 'Navigation',home: new FirstScreen(),);}}class FirstScreen extends StatelessWidget {@overrideWidget build(BuildContext context) {return new Container(color: Colors.blue,child: new Center(child: new RaisedButton(child: new Text('Launch second screen'),onPressed: () {Navigator.push(context,// 传入一个 routenew MaterialPageRoute(builder: (context) => new SecondScreen()),);},),),);}}class SecondScreen extends StatelessWidget {@overrideWidget build(BuildContext context) {return new Container(color: Colors.cyan,child: new Center(child: new RaisedButton(child: new Text('Go back!'),onPressed: () {Navigator.pop(context);},),),);}}
传输参数
import 'package:flutter/foundation.dart';import 'package:flutter/material.dart';class Todo {final String title;final String description;Todo(this.title, this.description);}void main() {runApp(MaterialApp(title: 'Passing Data',home: TodosScreen(todos: List.generate(20,(i) => Todo('Todo $i','A description of what needs to be done for Todo $i',),),),));}class TodosScreen extends StatelessWidget {final List<Todo> todos;TodosScreen({Key key, @required this.todos}) : super(key: key);@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text('Todos'),),body: ListView.builder(itemCount: todos.length,itemBuilder: (context, index) {return ListTile(title: Text(todos[index].title),onTap: () {Navigator.push(context,MaterialPageRoute(builder: (context) => DetailScreen(todo: todos[index]),),);},);},),);}}class DetailScreen extends StatelessWidget {final Todo todo;DetailScreen({Key key, @required this.todo}) : super(key: key);@overrideWidget build(BuildContext context) {// Use the Todo to create the UI.return Scaffold(appBar: AppBar(title: Text(todo.title),),body: Padding(padding: EdgeInsets.all(16.0),child: Text(todo.description),),);}}
利用 Material App 创建路由:
import 'package:flutter/material.dart';void main() {runApp(MaterialApp(debugShowCheckedModeBanner: false,title: 'DEMO',initialRoute: '/',routes: {'/': (context) => FirstScreen(),'/second': (context) => SecondScreen()},));}class FirstScreen extends StatelessWidget {@overrideWidget build(BuildContext context) {print(context);print('hello');return Scaffold(appBar: AppBar(title: Text('first screen'),),body: Center(child: RaisedButton(child: Text('Launch screen'),onPressed: () {// 使用命名路由跳转到第二个界面Navigator.pushNamed(context, '/second');},),),);}}class SecondScreen extends StatelessWidget {@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text('Second screen'),),body: Center(child: RaisedButton(onPressed: () {// 通过从堆栈弹出当前路由Navigator.pop(context);},child: Text('go back'),),),);}}
参考:
- Flutter 路由原理解析: https://juejin.im/post/5c8db8e8f265da2d864b889f
- Flutter 路由源码解析:https://juejin.im/post/5c8db8fff265da2de52dd80f
- https://medium.com/flutter-community/everything-you-need-to-know-about-flutter-page-route-transition-9ef5c1b32823
- https://blog.etby.org/2018/04/04/flutter-routes-and-navigation/
- https://book.flutterchina.club/chapter2/flutter_router.html
