Fun Animations in Flutter part -2

Hi and welcome back once again in this continued part of fun animations in flutter you can refresh your previous knowledge on flutter animation or if you missed it get through it

So as I promised to have some fun with animations in a flutter so I have decided to look back at elementary level of our schooling where we actually have been firstly creative in the early stage of our life yes the DRAWING class. Today we will consider this as an early class in the School Of Flutter Animations.

Before we get started let's look at what we are going to build.

Yes, a scenery of windmill with some Clouds, Birds, and Bee animating.

Stay cool everything is gonna be easy.

First, let's see our code,

Don't worry we are gonna see them in understandable snippets.

Since we are looking for animation in a flutter I assume you are familiar with the code above the WindMill widget.

Since here we are using multiple animations we are going to extend out widgets state class with TickerProviderStateMixin, For the case of a single animation, we would prefer SingleTickerProviderStateMixin.

here we are using three different controllers and their name clears the purpose they are used for. Further, we have four different values of class Animation holding the values of double this is simply the tween range for which they have to play the animation.

Now let's look at how they are set up to perform an animation.

initState is the place where logically we should set up our initialization logic and listeners in a StatefulWidget

first let's look at windmillController and windmillAnimation

we have initialized the controller with the two important parameters of vsync and duration as discussed in the previous article, now let's talk about the initialization of windmillAnimation.

Since this is the windmill we know this can rotate 360 degrees in both clockwise and anticlockwise direction so we initialize this simply with the TWEEN<double> from the beginning value of 0 and ending value to 2 * pi (360 degrees) in radians and provide it with the controller to animate, depending upon the status of animation we rotate this forward and reverse, Note the last line where we declare the controller to move forward this is what kicks our animation to start and rest is the work of our

to perform clockwise and anticlockwise animation.

here in bee animation same logic is applied as above with only as small change of introducing the curve

by default the animations in flutter are linear if no curve is provided means it is a linear animation else for some different type you can check

Coming to Cloud and bird animation

here we are using two animation class one for bird and cloud and other for moving left to right we did this here just to add on some detail to the animation of bird to see this as a bird taking a flight from bottom to top in the sky here for the fly animation our starting point is 0 while our ending point is -100 this is so our bird fly up in sky for positive value it will take a flight down. Other animation is only to move from left to right, and we say our controller to repeat the animation continuously.

And now the important thing dispose of all your controllers.

dispose controller in order to avoid memory leaks.

Now lets come to build method

Here we have used Stack so to create different details by overlapping the widgets on top of one another the background is created by Column with two Containers divided into Sky and Ground.

Lets keep this short and focus on only important Ones.

First is BirdBuilder

here we pass three required parameter namely - control for birdAndCloudController, linear for leftToRight and fly for flyAnimation

Our AnimatedBuilder will take rebulids itself with ever ticker tick from TickerProviderStateMixin probably 60 fps acc to flutter docs with every new ticks will generate a new value for each animations and our Stack will rebuild with new left value and Transfrom.translate will rebuild with new Offset value and will result in desired animation.

WindmillBuilder

Here we need to pass a required parameter of windmillAnimation.

Remember about the animation optimization we achived in our previous tutroial by cache our widget subtree we finally got the chance to apply this over here were we can see in animated builder we have assigned child attribute with some nested widgets this is what get cached and our builder functions only rebuilds RotationTransition and not its child which is our nested widgets. Here we have use a RotatedBox and made its child to turn one quarter this is because it dosen’t overlap the container ( blades of windmill ) above it which will cause it to look one even when there are two.

CloudBuilder

Over here our cloud builder takes two arugments control for birdAndCloudController second animation for leftToRightanimation.

and the builder rebuilds with new Positioned left and a new offset value for Transform.translate.

BeeBuilder

Here One again the logic gets repeated.

So finally we have completed our windmill.

Our next Drawing class, we will learn to hold the paintbrush and use our canvas to draw things.