Flutter Animated Series : Animated Containers
--
I always wanted to work with neat or crazy animations. I have worked with Android for a couple of years and I was starting to lose my love for animations, because trying to develop them in Android was a huge investment of my time.
Enter Flutter!
And I have fallen in love again.
To celebrate this newfound love, I am starting an Animated Series for Flutter.
YES It’s a SERIES!
I am going to cover everything about animation in Flutter from implicit animations to explicit animations and even Flare! This is the first one in the series.
Animated Containers
Animated Containers are implicit animated widgets.
Wait a second. What are implicit animated widgets again?
So usually to achieve an animation in Flutter, we have to define the Animation
and AnimationController
objects to manage the animations. There are other complex additions like TickerProviderMixin
. You have to manually start the animation, end the animation and various other complexities.
But good news for us, implicit animated widgets have all these logic under the hood, and we don’t have to worry about a thing. Give a duration
, define the beginning
and end
values and let your widgets animaaate!
Example 1 — Bar Charts
Imagine you have to build an application with bar graphs. So there can be a container with increasing or decreasing height depending on the graph values.
Let’s imagine the initial height of your Container
is 40.0
var height = 40.0;
And then your Container
will be like —
Container(
width: 60.0,
height: height,
color: Color(0xff14ff65),
),
And now suppose you have a Button
and in the onPressed
method, you can change the height of the Container
and setState()
to rebuild the UI.
onPressed: (){
setState(() {
height = 320.0;
});
},
But sadly, we have this following output.
There is no smooth transition when we change the height values. So this looks weird for a bar chart application.
We want the bar to gradually increase with time.
Let’s see if we can achieve our desired effect with AnimatedContainer
.
Let’s see some magic!
Replace your ordinary Container
with AnimatedContainer
and provide a Duration
property and see the magic happen!
AnimatedContainer(
duration: Duration(seconds: 5),
width: 60.0,
height: height,
color: Color(0xff14ff65),
)
One widget replacement and one extra line of code, and voila! You are animating a Container now!
I showed this example during my DevFest BBSR and DevFest Kolkata talks, and in minutes, Flutter was sold to my audience.
Example 2 — Take Flight
Obviously you can animate the usual Container
properties like color
, height
, and width
. But you can also animate a Container
’s child, like using the alignment
property to align a Container
’s child.
Container
’s child is an Icon
.
Alignment
property affects the child of a Container
The initial alignment is Alignment.bottomCenter
On clicking the button, we change the alignment
to Alignment.center
And the airplane flies right to the center of the screen!
You can find the entire working code here —
Example 3 — Playing with lots of colors!
Not one color, but many colors. We are going to animate the gradient
property of the Container
.
Here I defined just two colors and their initial alignment
and changed both the properties on setState()
.
Code here —
That’s it.
What else do we have in the Animated Series ?
- AnimatedContainers
- AnimatedOpacity
- AnimatedCrossFade (Coming soon)
- And many more…
Read the Spanish translation by CarlosMillan here —
Read the Portuguese translation by Pedro Massango here —
My articles are free, but you know you can press the clap👏 button 50 times? The higher you go, the more it motivates me to write more stuff for you guys!
Feeling super generous? Buy me a cupcake. 😋
Hello World, I am Pooja Bhaumik. A creative developer and a logical designer. You can find me on Linkedin or stalk me on GitHub or maybe follow me on Twitter? If that’s too social for you, just drop a mail to pbhaumik26@gmail.com if you wish to talk tech with me.
Have a nice fluttery day!