前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Flutter “跳转页面”(二)前言正文

Flutter “跳转页面”(二)前言正文

作者头像
坑吭吭
发布2018-08-31 17:36:48
1.5K0
发布2018-08-31 17:36:48
举报

前言

写了这么多文章,有翻译文档的,有自己理解的,也不知道到底是怎么样的风格更能让人接受,希望大家能给点意见或建议。

正文

一里面已经把“页面跳转”的操作基本上都说完了,那当一个页面打开后是需要一些返回信息的时候应该怎么做呢?还是继续看原文吧

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's result parameter. For example if we wanted to ask the user to press 'OK' to confirm an operation we could await the result of Navigator.push:

当一个route加载到页面并需要返回一个值的时候,这个值会在pop方法返回 Navigatorpush方法会返回一个Future,这个东西可以在dart教程里面找到。这个Future会在routepop的时候处理,而这个Future的值就是pop方法里的result参数。 比如,当我们想在用户点击ok的时候去确认一个操作,这个时候我们可以await这个push的结果。await的用法也可以在dart的教程里找到。

代码语言:javascript
复制
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 of MaterialPageRoute<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。这些控件也通过Navigatorpushpop来控制显示消失。

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 specifies opaque: false, just as a popup route does.

你可以创建任何一个route的子类,并控制他们的动画、颜色、行为等等。PageRouteBuilder类用各种回调来控制这些,下面的例子是一个页面以旋转的方式来显示和隐藏。这个route不会让整个屏幕变灰,因为他指定了opaque:false

代码语言:javascript
复制
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,
      ),
    );
  }
));
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.07.27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 正文
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档