前言
写了这么多文章,有翻译文档的,有自己理解的,也不知道到底是怎么样的风格更能让人接受,希望大家能给点意见或建议。
正文
一里面已经把“页面跳转”的操作基本上都说完了,那当一个页面打开后是需要一些返回信息的时候应该怎么做呢?还是继续看原文吧
Routes can return a value route们可以返回一个值
When a route is pushed to ask the user for a value, the value can be returned via the pop method’s result parameter.
Methods that push a route return a Future. The Future resolves when the route is popped and the Future‘s value is the pop method’sresult
parameter.
For example if we wanted to ask the user to press ‘OK’ to confirm an operation we couldawait
the result of Navigator.push:
当一个route
加载到页面并需要返回一个值的时候,这个值会在pop
方法返回
Navigator
的push
方法会返回一个Future
,这个东西可以在dart
教程里面找到。这个Future
会在route
被pop
的时候处理,而这个Future
的值就是pop
方法里的result
参数。
比如,当我们想在用户点击ok
的时候去确认一个操作,这个时候我们可以await
这个push
的结果。await
的用法也可以在dart
的教程里找到。
bool value = await Navigator.push(context, new MaterialPageRoute<bool>(
builder: (BuildContext context) {
return new Center(
child: new GestureDetector(
child: new Text('OK'),
onTap: () { Navigator.pop(context, true); }
),
);
}
));
上面的代码,如果用户按了ok
键,则返回的值是true,如果用户是按的返回键,则返回值是null
When a route is used to return a value, the route’s type parameter must match the type of pop‘s result. That’s why we’ve used
MaterialPageRoute<bool>
instead ofMaterialPageRoute<void>
or justMaterialPageRoute
. (If you prefer to not specify the types, though, that’s fine too.)
当一个route
被用来返回一个值的时候,这个route
的参数类型必须和结果返回的类型一致,这就是为什么我们用MaterialPageRoute<bool>
而不用MaterialPageRoute<void>
或直接用MaterialPageRoute
。(如果你就不想指定返回类型,也是可以的)
Popup routes “弹出route”
Routes don’t have to obscure the entire screen. PopupRoutes cover the screen with a ModalRoute.barrierColor that can be only partially opaque to allow the current screen to show through. Popup routes are “modal” because they block input to the widgets below.
There are functions which create and show popup routes. For example: showDialog, showMenu, and showModalBottomSheet. These functions return their pushed route’s Future as described above. Callers can await the returned value to take an action when the route is popped, or to discover the route’s value.
There are also widgets which create popup routes, like PopupMenuButton and DropdownButton. These widgets create internal subclasses of PopupRoute and use the Navigator’s push and pop methods to show and dismiss them.
Route
并不一定非要跟屏幕一样大,弹窗可以用ModalRoute.barrierColor
使部分当前屏幕不透明,用来显示内容。弹窗是一种“模态”窗口,因为他锁定了他下面的控件。这里有一些创建和显示弹窗的方法。比如: showDialog, showMenu, 和 showModalBottomSheet。这些方法的返回的Future
和上面所说的是一样的。当这个弹窗出栈的时候,调用者可以通过await
拿到这个返回值。
这里还有一些其他的方法来创建弹窗。比如:PopupMenuButton and DropdownButton。这些控件也通过Navigator
的push
和pop
来控制显示消失。
Custom routes 自定义route
You can create your own subclass of one of the widget library route classes like PopupRoute, ModalRoute, or PageRoute, to control the animated transition employed to show the route, the color and behavior of the route’s modal barrier, and other aspects of the route.
The PageRouteBuilder class makes it possible to define a custom route in terms of callbacks. Here’s an example that rotates and fades its child when the route appears or disappears. This route does not obscure the entire screen because it specifiesopaque: false
, just as a popup route does.
你可以创建任何一个route
的子类,并控制他们的动画、颜色、行为等等。PageRouteBuilder
类用各种回调来控制这些,下面的例子是一个页面以旋转的方式来显示和隐藏。这个route
不会让整个屏幕变灰,因为他指定了opaque:false
:
Navigator.push(context, new PageRouteBuilder(
opaque: false,
pageBuilder: (BuildContext context, _, __) {
return new Center(child: new Text('My PageRoute'));
},
transitionsBuilder: (___, Animation<double> animation, ____, Widget child) {
return new FadeTransition(
opacity: animation,
child: new RotationTransition(
turns: new Tween<double>(begin: 0.5, end: 1.0).animate(animation),
child: child,
),
);
}
));