所以我在 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,
);
}
)
Run Code Online (Sandbox Code Playgroud)
该pageBuilder
属性需要一个名为的typedef RoutePageBuilder并为transitionsBuilder
财产被称为typedef的RouteTransitionsBuilder预期。
对我来说,看起来我只是对那些带有来自 typedef 的预定义参数的属性使用一个函数,但我不确定这一点。
另外,这里的输入实际上是什么参数?因为我已经例如用作Animation<double> animation1
参数但实际上并没有创建一个 new Animation
,或者是吗?如果不是,那么Animation
实际将作为参数传递什么?
typedef 可用于指定我们希望特定函数匹配的函数签名。函数签名由函数的参数(包括它们的类型)定义。返回类型不是函数签名的一部分。其语法如下。
在您的情况下, PageRouteBuilder 将作为参数:
两个 typedef(RouteTransitionsBuilder 和 RoutePageBuilder)就像单个方法的接口(面向对象编程)。在这种情况下,typedef RoutePageBuilder 强制您将一个函数作为参数传递,该函数返回一个以上下文和两个 Animation 作为参数的 Widget。
如果您想了解有关该函数中传递的参数的更多详细信息,您可以在 flutter 的文档中查看它,例如:
动画片 ?动画 驱动路线过渡和前一路线向前过渡的动画。只读,继承
或者
二次动画?动画 被推到这条路线顶部的路线的动画。这个动画让这条路线与推到这条路线顶部的路线的入口和出口过渡协调。只读,继承
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);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4134 次 |
最近记录: |