Navigator 路由原理

我们先来看看一个普通 App 路由会有什么样结构:
image.png

  • 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 纯类包囊了我们的 MyPage widget,它的作用是在看这个widget 是否需要绘制,和保持它的状态,为什么要用纯类不直接操作 widget?这就是聪明的地方如果 MyPage2 不需被创建 我们是否不应该将它加入到 Element 树 里面?。以下是这个类有的属性:
      • builder: widget build 引用。
      • opaque:是否全屏不透明,如果是 true 那么下面的一层就不需要绘制了。
      • maintainState:这个 widget 我们要不要它活着,打个比方 我们的 MyPage1 做了一些操作去到了 MyPage2 上面,而且完全的覆盖了 MyPage1 ,那么问题来了,如果现在 MyPage1 不绘制我们需不需要维持它的状态?这就是这个属性的作用!
  • _Theatre: 里面有个 theatre (剧院),剧院里有 onstage(舞台)和 offstage(观众席),舞台上是只在被显示的组件。观众席上是那些没被显示但 mantainState = true 的组件。

    • onstage(舞台)里面有个 stack widget 它是用来处理 widget 的堆叠。

image.png
Overlay 和 Theatre 关系

来一个比方:我们定义了 Dialog(弹窗),page3, page2 和 page1,我们打开App 进入了 page3 然后在出现一个 Dialog 弹窗,这时候我们 App 的状态应该是这样的,

  1. Overlay 定义了 dialog, page3,page2 和 page1 的OverlayEntity
  2. 我们歌剧院里有dialog, page3,page2 和 page1
  3. 舞台上有dialog, page3,因为弹窗是透明的所以 page3 需要被绘制
  4. 观众席上有 page2 和 page1,因为它们被page 3 挡住了不需要被绘画所以我们只 build 它们。

image.png

用法

一个简单的例子:

如果你看 MaterialPageRoute
MaterialPageRoute继承自PageRoute类,PageRoute是一个抽象类,表示占有整个屏幕空间的一个模态路由页面,它还定义了路由构建及切换时过渡动画的相关接口及属性。MaterialPageRoute 是Material组件库提供的组件,它可以针对不同平台,实现与平台页面切换动画风格一致的路由切换动画:

  1. import 'package:flutter/material.dart';
  2. void main() => runApp(new MyApp());
  3. class MyApp extends StatelessWidget {
  4. @override
  5. Widget build(BuildContext context) {
  6. return new MaterialApp(
  7. title: 'Navigation',
  8. home: new FirstScreen(),
  9. );
  10. }
  11. }
  12. class FirstScreen extends StatelessWidget {
  13. @override
  14. Widget build(BuildContext context) {
  15. return new Container(
  16. color: Colors.blue,
  17. child: new Center(
  18. child: new RaisedButton(
  19. child: new Text('Launch second screen'),
  20. onPressed: () {
  21. Navigator.push(
  22. context,
  23. // 传入一个 route
  24. new MaterialPageRoute(builder: (context) => new SecondScreen()),
  25. );
  26. },
  27. ),
  28. ),
  29. );
  30. }
  31. }
  32. class SecondScreen extends StatelessWidget {
  33. @override
  34. Widget build(BuildContext context) {
  35. return new Container(
  36. color: Colors.cyan,
  37. child: new Center(
  38. child: new RaisedButton(
  39. child: new Text('Go back!'),
  40. onPressed: () {
  41. Navigator.pop(context);
  42. },
  43. ),
  44. ),
  45. );
  46. }
  47. }

传输参数

  1. import 'package:flutter/foundation.dart';
  2. import 'package:flutter/material.dart';
  3. class Todo {
  4. final String title;
  5. final String description;
  6. Todo(this.title, this.description);
  7. }
  8. void main() {
  9. runApp(MaterialApp(
  10. title: 'Passing Data',
  11. home: TodosScreen(
  12. todos: List.generate(
  13. 20,
  14. (i) => Todo(
  15. 'Todo $i',
  16. 'A description of what needs to be done for Todo $i',
  17. ),
  18. ),
  19. ),
  20. ));
  21. }
  22. class TodosScreen extends StatelessWidget {
  23. final List<Todo> todos;
  24. TodosScreen({Key key, @required this.todos}) : super(key: key);
  25. @override
  26. Widget build(BuildContext context) {
  27. return Scaffold(
  28. appBar: AppBar(
  29. title: Text('Todos'),
  30. ),
  31. body: ListView.builder(
  32. itemCount: todos.length,
  33. itemBuilder: (context, index) {
  34. return ListTile(
  35. title: Text(todos[index].title),
  36. onTap: () {
  37. Navigator.push(
  38. context,
  39. MaterialPageRoute(
  40. builder: (context) => DetailScreen(todo: todos[index]),
  41. ),
  42. );
  43. },
  44. );
  45. },
  46. ),
  47. );
  48. }
  49. }
  50. class DetailScreen extends StatelessWidget {
  51. final Todo todo;
  52. DetailScreen({Key key, @required this.todo}) : super(key: key);
  53. @override
  54. Widget build(BuildContext context) {
  55. // Use the Todo to create the UI.
  56. return Scaffold(
  57. appBar: AppBar(
  58. title: Text(todo.title),
  59. ),
  60. body: Padding(
  61. padding: EdgeInsets.all(16.0),
  62. child: Text(todo.description),
  63. ),
  64. );
  65. }
  66. }

利用 Material App 创建路由:

  1. import 'package:flutter/material.dart';
  2. void main() {
  3. runApp(MaterialApp(
  4. debugShowCheckedModeBanner: false,
  5. title: 'DEMO',
  6. initialRoute: '/',
  7. routes: {
  8. '/': (context) => FirstScreen(),
  9. '/second': (context) => SecondScreen()
  10. },
  11. ));
  12. }
  13. class FirstScreen extends StatelessWidget {
  14. @override
  15. Widget build(BuildContext context) {
  16. print(context);
  17. print('hello');
  18. return Scaffold(
  19. appBar: AppBar(
  20. title: Text('first screen'),
  21. ),
  22. body: Center(
  23. child: RaisedButton(
  24. child: Text('Launch screen'),
  25. onPressed: () {
  26. // 使用命名路由跳转到第二个界面
  27. Navigator.pushNamed(context, '/second');
  28. },
  29. ),
  30. ),
  31. );
  32. }
  33. }
  34. class SecondScreen extends StatelessWidget {
  35. @override
  36. Widget build(BuildContext context) {
  37. return Scaffold(
  38. appBar: AppBar(
  39. title: Text('Second screen'),
  40. ),
  41. body: Center(
  42. child: RaisedButton(
  43. onPressed: () {
  44. // 通过从堆栈弹出当前路由
  45. Navigator.pop(context);
  46. },
  47. child: Text('go back'),
  48. ),
  49. ),
  50. );
  51. }
  52. }

参考: