代码之家  ›  专栏  ›  技术社区  ›  Bram Vanbilsen

Dart中的Typedefs

  •  5
  • Bram Vanbilsen  · 技术社区  · 8 年前

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

    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 对于 transitionsBuilder 属性调用了typedef RouteTransitionsBuilder 应为。

    对我来说,看起来我只是在使用一个函数来处理那些属性,这些属性具有来自typedef的预定义参数,但我对此不确定。
    还有,什么参数实际上是这里的输入?因为我举个例子 Animation<double> animation1 作为一个参数,但这实际上不会创建一个新的 Animation ,还是这样?如果没有,怎么办 动画 将实际作为参数传递?

    2 回复  |  直到 8 年前
        1
  •  6
  •   Antonino    8 年前

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

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

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

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

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

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

    或

    secondaryAnimation动画管线的动画 被推到这条路线的顶端。此动画允许此路线 与推进路线的入口和出口过渡相协调 这条路线的顶端。 只读,继承

    PS:如果你通过颤振代码导航并到达路线。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);
    
        2
  •  3
  •   Peter Haddad    7 年前

    从 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() 是一个小部件。