【问题标题】:Typedefs in DartDart 中的类型定义
【发布时间】:2018-07-30 03:31:11
【问题描述】:

所以我在 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&lt;double&gt; animation1 作为参数,但实际上并没有创建一个新的Animation,或者是吗?如果没有,Animation 实际上会作为参数传递吗?

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

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

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

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

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

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

    animation → Animation 驱动路线的动画 过渡和前一个路由的前向过渡。 只读,继承

    secondaryAnimation → Animation 路线的动画 被推到这条路线的顶部。这个动画让这条路线 与推送的路线的入口和出口过渡相协调 这条路线的顶部。 只读,继承

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

    /// 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);
    

    【讨论】:

    • 谢谢!一个小问题:我怎么知道哪些参数被填写?例如,BuildContext context 参数被填写,但如何填写以及在哪里填写?什么处理?
    【解决方案2】:

    来自docs

    在 Dart 中,函数是对象,就像字符串和数字是对象一样。 typedef 或函数类型别名(也称为)为函数类型提供了一个名称,您可以在声明字段和返回类型时使用该名称。将函数类型分配给变量时,typedef 会保留类型信息。

    如果你检查RoutePageBuilder的实现例如:

    typedef RoutePageBuilder = Widget Function(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation); 
    

    这意味着RoutePageBuilder 也称为Function,它返回Widget

    那么如果你查看PageRouteBuilder的源代码,你会发现实例变量pageBuilder的类型是RoutePageBuilder,因此在创建PageRouteBuilder的新实例时可以这样做:

    new PageRouteBuilder(
      pageBuilder: (BuildContext context, Animation<double> animation1, Animation<double> animation2) => new Test(),
    

    这里new Test() 是一个小部件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-06
      • 2012-09-14
      • 1970-01-01
      • 2021-10-14
      • 2021-10-04
      • 2021-06-25
      • 2013-05-09
      • 2020-06-05
      相关资源
      最近更新 更多