Dart中的Typedef

布拉姆·范比尔森

因此,我已经在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属性需要一个名为的typedef RoutePageBuilder并为transitionsBuilder财产被称为typedef的RouteTransitionsBuilder预期。

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

安东尼·卡塞斯

typedef可用于指定我们希望特定功能匹配的功能签名。函数签名由函数的参数(包括其类型)定义。返回类型不是函数签名的一部分。其语法如下。

在您的情况下,PageRouteBuilder采用以下参数:

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

两种typedef(RouteTransitionsBuilder和RoutePageBuilder)就像(面向对象编程的)接口,用于单个方法。在这种情况下,typedef RoutePageBuilder会强制您将返回一个以上下文和两个Animation作为参数的Widget的函数作为参数传递。

如果需要有关该函数中传递的参数的更多详细信息,可以在flutter的文档中进行检查,例如:

动画→动画驱动路线的过渡和上一条路线的前向过渡的动画。只读,继承

要么

secondaryAnimation→动画在此路线上推入路线的动画。此动画使此路线与推入此路线顶部的路线的入口和出口过渡相协调。只读,继承

PS:如果您浏览颤动代码并到达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);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章