T extends dynamic> class
A linear interpolation between a beginning and ending value.
Tween is useful if you want to interpolate across a range.
You can chain Tween objects together using the chain method, so that a single Animation object is configured by multiple Tween objects called in succession. This is different than calling the animate method twice, which results in two separate Animation objects, each configured with a single Tween.
_controlleris an AnimationController, and we want to create an Animation<Offset> that is controlled by that controller, and save it in
_animation. Here are two possible ways of expressing this:
_animation = _controller.drive( Tween<Offset>( begin: const Offset(100.0, 50.0), end: const Offset(200.0, 300.0), ), );
_animation = Tween<Offset>( begin: const Offset(100.0, 50.0), end: const Offset(200.0, 300.0), ).animate(_controller);
In both cases, the
_animation variable holds an object that, over the
lifetime of the
_controller's animation, returns a value
_animation.value) that depicts a point along the line between the two
offsets above. If we used a MaterialPointArcTween instead of a
Tween<Offset> in the code above, the points would follow a pleasing curve
instead of a straight line, with no other changes necessary.
Tweens are mutable; specifically, their begin and end values can be changed at runtime. An object created with Animation.drive using a Tween will immediately honor changes to that underlying Tween (though the listeners will only be triggered if the Animation is actively animating). This can be used to change an animation on the fly without having to recreate all the objects in the chain from the AnimationController to the final Tween.
If a Tween's values are never changed, however, a further optimization can
be applied: the object can be stored in a
static final variable, so that
the exact same instance is used whenever the Tween is needed. This is
preferable to creating an identical Tween afresh each time a State.build
method is called, for example.
Types with special considerations
Types that define
- operators to combine values (
T + T → T and
T - T → T) and an
* operator to scale by multiplying with a double (
T * double → T) can be directly used with
This does not extend to any type with
* operators. In
particular, int does not satisfy this precise contract (
int * double
actually returns num, not int). There are therefore two specific classes
that can be used to interpolate integers:
- IntTween, which is an approximation of a linear interpolation (using double.round).
- StepTween, which uses double.floor to ensure that the result is
never greater than it would be using if a
In addition, some of the types that do have suitable
operators still have dedicated Tween subclasses that perform the
interpolation in a more specialized manner. One such class is
MaterialPointArcTween, which is mentioned above. The AlignmentTween, and
AlignmentGeometryTween, and FractionalOffsetTween are another group of
Tweens that use dedicated
lerp methods instead of merely relying on the
operators (in particular, this allows them to handle null values in a more
- begin ↔ T
The value this variable has at the beginning of the animation. [...]
read / write
- end ↔ T
The value this variable has at the end of the animation. [...]
read / write
- hashCode → int
The hash code for this object. [...]
- runtimeType → Type
A representation of the runtime type of the object.
double t ) → T
Returns the value this variable has at the given animation clock value. [...]
) → String
Returns a string representation of this object.
double t ) → T
Returns the interpolated value for the current value of the given animation. [...]
Animation< double>parent ) → Animation< T>
Returns a new Animation that is driven by the given animation but that
takes on values determined by this object. [...]
Animatable< double>parent ) → Animatable< T>
Returns a new Animatable whose value is determined by first evaluating
the given parent and then evaluating this object. [...]
Animation< double>animation ) → T
The current value of this object for the given Animation. [...]
Invocation invocation ) → dynamic
Invoked when a non-existent method or property is accessed. [...]