VxAnimator
VelocityX gives you VxAnimator that makes animation easier.
important
VxAnimator uses animator package to support animations. You can use animator directly without VelocityX.
note
VxAnimator as of now doesn't work with null safety.
Above is an animated VxBox using the VxAnimator Widget
Using VxAnimator
Animator:
- makes animation as simple as the simplest widget in Flutter with the help of Animator widget,
- Allows you to control (forward, stop, inverse), reconfigure, reset and restart animation from buttons, or events callbacks.
Animator:
With one widget, Animator
, you can do all the available animation in Flutter.
Actually, Animator is a facade that hides all the complexity of the animation setting in Flutter. In the design pattern the facade pattern is :
Facade is a structural design pattern that provides a simplified interface to a library, a framework, or any other complex set of classes.
To implement any type of animation with animator you have to define a
Tween
(1),Duration
(2), andCurve
(3). `With
cycles
argument (4) you define the number of the forward and backward periods you want your animation to perform before stopping.With
repeats
argument (5) you define the number of forward periods you want your animation to perform before stopping.In the
builder
argument (6) you put your widgets to be animated.VxAnimator<T>(builder : (BuildContext context, AnimatorState animatorState, Widget child) {//to get animation value:final T value = animatorState.value;//to get Animation object:final Animation<T> animation = animatorState.animation;//to get AnimationController object:final AnimationController animation = animatorState.controller;//to get animation value form tweenMapfinal R value = animatorState.getValue<R>('animName');//To get Animation object from tweenMapfinal Animation<R> value =animatorState.getAnimation<R>('animName');},child : MayWidget(), //widget to not rebuild with animation).make()If you want to animate many Tween, use
tweenMap
argument (2). It is a Map of String type keys and Tween type values.With
endAnimationListener
(8) argument you can define a VoidCallback to be executed when the animation is finished. For example, it can be used to trigger another animation.With
customListener
(9) argument you can define a function to be called every time the animation value changes. The customListener is provided with an Animation object.With
statusListener
(10) argument, you can define a function to be called every time the status of the animation change. The customListener is provided with an AnimationStatus, AnimationSetup objects.triggerOnInit
(11) controls whether the animation is automatically started when the Animator widget is initialized. The default value is true.If you want to reset your animation, such as changing your Tween or duration, and want the new setting to be reconsidered when the Animator widget is rebuilt, set the
resetAnimationOnRebuild
(12) argument to true. The default value is false. (SeeAnimatorKey
)The right
TickerProvider
is chosen by animator:(13)- If animation is simple the
singleTickerProviderStateMixin
. - If you define the animatorKey parameter or set
resetAnimationOnRebuild
, animator usetickerProviderStateMixin
.
- If animation is simple the
With
animationKey
(14), you can associate anAnimator
widget to anAnimatorKey
. Doing so, you can control (forward, start, stop, reverse) animation from callbacks outside the Animator widget, you can even reset the animation parameter (tween, duration curve, repeats, cycles) on the fly. (See example below)
Example of a single Tween animation:
Above is the output of the code below
Example of a multi-tween animation:
Above is the output of the code below
You can nest many animators with a different setting (tween, duration, curve, repeats, and cycles) to do a complex animation:
This is a simple example of how to animate the scale and rotation independently.
Use nested Animators with CustomPainter and CustomClipper and draw the animation you want.
AnimatorKey
Similarly to Flutter global key. AnimatorKey allows controlling the animation from outside the Animator widget it is associated with.
Example:
AnimatorRebuilder widget
The AnimatorRebuilder widget can subscribe to an βAnimatorKeyβ. It will be animated in synchronization with the Animator widget with which animatorKey
is associated.
Example:
The order of Animator and AnimatorRebuilder in the widget tree is irrelevant, except in the case, AnimatorRebuilder is first inserted in the widget tree, you have to provide initial values in AnimatorKey to avoid null exceptions.