dart - Dart 中的类型定义

所以我在 Dart 中使用 typedef 已经有一段时间了,而没有考虑它们实际上是什么。所以例如在这段代码中:

new PageRouteBuilder(
  pageBuilder: (BuildContext context, Animation<double> animation1, Animation<double> animation2) => new Test(),
  transitionsBuilder: (BuildContext context, Animation<double> animation1, Animation<double> animation2, Widget child) {
    return new FadeTransition(
      child: child,
      opacity: animation1,
    );
  }
)

pageBuilder属性需要一个名为 RoutePageBuilder 的 typedef对于 transitionsBuilder一个名为 RouteTransitionsBuilder 的 typedef 属性是期待。

对我来说,看起来我只是对那些带有 typedef 预定义参数的属性使用了一个函数,但我对此不确定。
另外,这里的输入实际上是什么参数?因为例如我使用了Animation<double> animation1作为一个参数,但实际上并没有创建一个新的 Animation ,或者是吗?如果没有,那是什么 Animation实际上会作为参数传递吗?

最佳答案

A typedef can be used to specify a function signature that we want specific functions to match. A function signature is defined by a function’s parameters (including their types). The return type is not a part of the function signature. Its syntax is as follows.

在您的情况下, PageRouteBuilder 将作为参数:

  • pageBuilder RoutePageBuilder typedef - 只是一个与 RoutePageBuilder typedef 签名匹配的函数。
  • transitionsBuilder RouteTransitionsBuilder typedef - 与 pageBuilder 完全相同,但匹配 RouteTransitionsBuilder 签名。

这两个 typedef(RouteTransitionsBuilder 和 RoutePageBuilder)类似于单个方法的接口(interface)(OO 编程)。在这种情况下,typedef RoutePageBuilder 强制您将一个函数作为参数传递,该函数返回一个以上下文和两个动画作为参数的 Widget。

如果您想了解更多关于该函数中传递的参数的详细信息,您可以查看 Flutter 的文档,例如:

animation → Animation The animation that drives the route's transition and the previous route's forward transition. read-only, inherited

secondaryAnimation → Animation The animation for the route being pushed on top of this route. This animation lets this route coordinate with the entrance and exit transition of routes pushed on top of this route. read-only, inherited

PS:如果您浏览 flutter 代码并到达 routes.dart 文件,您可以找到记录这部分的注释:

/// Override this method to build the primary content of this route.
  ///
  /// The arguments have the following meanings:
  ///
  ///  * `context`: The context in which the route is being built.
  ///  * [animation]: The animation for this route's transition. When entering,
  ///    the animation runs forward from 0.0 to 1.0. When exiting, this animation
  ///    runs backwards from 1.0 to 0.0.
  ///  * [secondaryAnimation]: The animation for the route being pushed on top of
  ///    this route. This animation lets this route coordinate with the entrance
  ///    and exit transition of routes pushed on top of this route.
  ///
  /// This method is called when the route is first built, and rarely
  /// thereafter. In particular, it is not called again when the route's state
  /// changes. For a builder that is called every time the route's state
  /// changes, consider [buildTransitions]. For widgets that change their
  /// behavior when the route's state changes, consider [ModalRoute.of] to
  /// obtain a reference to the route; this will cause the widget to be rebuilt
  /// each time the route changes state.
  ///
  /// In general, [buildPage] should be used to build the page contents, and
  /// [buildTransitions] for the widgets that change as the page is brought in
  /// and out of view. Avoid using [buildTransitions] for content that never
  /// changes; building such content once from [buildPage] is more efficient.
  Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation);

https://stackoverflow.com/questions/48873934/

相关文章:

dart - 如何在弹出菜单中使用单选按钮?

flutter - 如何在指针向下事件的位置绘制 CustomPaint 小部件?

Flutter - 如何将图像定义转换为图标定义?

flutter - TabBarView 中的 GestureDetector - 嵌套滚动?

flutter - 如何在 Flutter Web 中保存到 Web 本地存储

dart - 如何为 Flutter 应用绘制折线图?

dart - 在 Flutter 应用程序中存储外观和指标常量

ios - 如何在 iOS 中安装用 Flutter 编写的应用程序?

dart - 如何在 flutter 中为图标添加阴影?

dart - StreamBuilder 会在无状态小部件中自动关闭流吗?