drive<U> method
- Animatable<
U> child
Chains a Tween (or CurveTween) to this Animation.
This method is only valid for Animation<double>
instances (i.e. when T
is double
). This means, for instance, that it can be called on
AnimationController objects, as well as CurvedAnimations,
ProxyAnimations, ReverseAnimations, TrainHoppingAnimations, etc.
It returns an Animation specialized to the same type, U
, as the
argument to the method (child
), whose value is derived by applying the
given Tween to the value of this Animation.
_controller
, the following code creates
an Animation<Alignment>
that swings from top left to top right as the
controller goes from 0.0 to 1.0:
Animation<Alignment> alignment1 = _controller.drive(
AlignmentTween(
begin: Alignment.topLeft,
end: Alignment.topRight,
),
);
alignment1.value
could then be used in a widget's build method, for
instance, to position a child using an Align widget such that the
position of the child shifts over time from the top left to the top right.
It is common to ease this kind of curve, e.g. making the transition slower at the start and faster at the end. The following snippet shows one way to chain the alignment tween in the previous example to an easing curve (in this case, Curves.easeIn). In this example, the tween is created elsewhere as a variable that can be reused, since none of its arguments vary.
final Animatable<Alignment> tween = AlignmentTween(begin: Alignment.topLeft, end: Alignment.topRight)
.chain(CurveTween(curve: Curves.easeIn));
// ...
final Animation<Alignment> alignment2 = _controller.drive(tween);
Animation<Alignment> alignment3 = _controller
.drive(CurveTween(curve: Curves.easeIn))
.drive(AlignmentTween(
begin: Alignment.topLeft,
end: Alignment.topRight,
));
Animation<Color> color = Animation<double>.fromValueListenable(_scrollPosition)
.drive(Animatable<Color>.fromCallback((double value) {
return Color.fromRGBO(value.round() % 255, 0, 0, 1);
}));
See also:
- Animatable.animate, which does the same thing.
- AnimationController, which is usually used to drive animations.
- CurvedAnimation, an alternative to CurveTween for applying easing curves, which supports distinct curves in the forward direction and the reverse direction.
- Animatable.fromCallback, which allows creating an Animatable from an arbitrary transformation.
Implementation
@optionalTypeArgs
Animation<U> drive<U>(Animatable<U> child) {
assert(this is Animation<double>);
return child.animate(this as Animation<double>);
}