The web content provides an overview of implicitly animated widgets in Flutter, demonstrating how developers can simplify animations using widgets like AnimatedContainer, Hero Animations, AnimatedCrossFade, and AnimatedOpacity, without the need for explicit controllers or tweens.
Abstract
Flutter's framework simplifies the process of creating animations by offering a suite of implicitly animated widgets. These widgets allow for smooth transitions between UI states with minimal code, as seen with the AnimatedContainer, which automatically animates changes in properties like color and size. Hero Animations enable seamless transitions of UI elements across different screens, enhancing the user experience with a 'flying' effect. The AnimatedCrossFade widget facilitates a smooth switch between two different widgets, while AnimatedOpacity provides a straightforward way to animate changes in a widget's visibility. The article emphasizes the ease of use and efficiency of these widgets, suggesting that many developers are not fully aware of their capabilities, which can lead to unnecessary complexity in animation implementation.
Opinions
The author believes that animations in Flutter are easier to achieve compared to native Android development, due to the availability of inbuilt widgets that handle complex animations with simple value changes.
There is an opinion that developers often overlook the power of implicitly animated widgets, leading to wasted effort in animation creation.
The author suggests that in many cases, the default behavior of widgets like AnimatedContainer is sufficient and can eliminate the need for defining custom animations with controllers and tweens.
The author expresses enthusiasm about the effectiveness of Hero Animations in creating engaging transitions between different parts of an app.
It is implied that the use of AnimatedCrossFade and AnimatedOpacity can significantly reduce the amount of code required to produce high-quality animations in Flutter applications.
The author encourages readers to follow them for more insights on Flutter and invites feedback on the article, indicating a commitment to community engagement and continuous learning.
Implicitly Animated Widgets in Flutter
Simplifying animations in Flutter with Implicitly Animated Widgets
Animations are quite easy to do in Flutter and a lot of complexity can be achieved with a lot less effort than native Android. This is usually achieved through ways like defining an Animation + an AnimationController. But there are inbuilt widgets which even reduce this and make animations as easy as simply changing values!
The complete examples will be hosted on my Github page given at the end of this article.
AnimatedContainer
An AnimatedContainer in Flutter
An AnimatedContainer automatically transitions to the values defined (colors, sizes etc.) instead of just instantaneously changing to them. The GIF given above is an example of an AnimatedContainer.
An AnimatedContainer is defined as:
var myColor = Colors.blue;
var myHeight =100.0;
var myWidth =100.0;
In normal cases, you would define a controller and a Tween and a ColorTween to achieve the animation in the GIF above. But with AnimatedContainer, all you have to do is:
Set a duration for the animation
duration: Duration(seconds: 1),
2. Change values (Change color and size to your values)
The moment you change the values of myColor, my Height or myWidth and setState(), the container automatically transitions to the values instead of changing directly to that value.
At the beginning, myColor was set to Colors.blue. When we change it to Colors.green and setState to rebuild it, it transitions from blue to green without any Tweens used. (Note: Tweens are used implicitly but do not need to be user defined.)
From what I’ve seen, a lot of developers don’t seem to be aware of these implicitly animated widgets and waste time when it’s not necessary. Obviously there will be cases where you might want a different kind of behaviour, but most times, AnimatedContainer does the job.
Hero Animations
Hero Animations in Flutter
A Hero animation makes an element from one page “fly” to the second and automatically adjust to the size of the same element on the second page. This makes something like a list in an app which has a detail page that much more interesting.
Flutter makes it incredibly easy to implement Hero animations. All you have to do is wrap the element you want to make a ‘Hero’ with a Hero widget and supply to it a tag. If you want to do this in a list, you need to supply a different tag for each element, which I just usually set to “hero” + position of element in the list.
The above example has a simple card on the first page with a container with a red color.
A CrossFade is a smooth transition from one widget to another with a given duration. Flutter makes this easy using a AnimatedCrossFade widget.
This is how an AnimatedCrossFade is defined:
AnimatedCrossFade(
firstChild: // Your first element here,secondChild: // Element after transition,crossFadeState: // State of the transition,duration: Duration(milliseconds: 1500),
),
This amount of code is enough to give us the above example. The transition is simply triggered by changing the crossFadeState.
Note: If the two children are of different sizes then it automatically transitions from one size to the other.
Here’s a demo:
AnimatedOpacity
AnimatedOpacity animates changes in opacity, i.e. how visible a widget is. An opacity of 0 makes a widget completely transparent, while a 1 opacity is fully visible.
AnimatedOpacity is declared as:
AnimatedOpacity(
opacity: // Your variable holding opacity value, duration: // Duration of the transition, child: FlutterLogo(),
)
Staying similar to the last few widgets, in AnimatedOpacity, you change the opacity and setState and it automatically animates the opacity change.
That’s it for this article! I hope you enjoyed it and leave a few claps if you did. Follow me for more Flutter articles and comment for any feedback you might have about this article.