Flutter Animations: A Guide to Implicit and Explicit Animations
- Get link
- X
- Other Apps
Flutter Animations: A Guide to Implicit and Explicit Animations
Flutter offers powerful tools to add smooth and beautiful animations in your apps. Animations improve the user experience and make the UI more engaging.
π§© Types of Animations in Flutter
There are two main types:
Implicit Animations – Simple, automatic transitions
Explicit Animations – Fully controlled and customizable
πΉ 1. Implicit Animations
✅ What Are They?
Implicit animations automatically animate changes in widget properties like:
Size
Color
Opacity
Position
You don’t need to manage controllers or animation logic.
π§ How It Works
Flutter detects changes in values and smoothly transitions between them.
π¦ Common Implicit Animation Widgets
Widget | What It Animates |
---|---|
AnimatedContainer | Size, color, padding, etc. |
AnimatedOpacity | Opacity (fade in/out) |
AnimatedAlign | Alignment |
AnimatedPositioned | Position inside a Stack |
AnimatedDefaultTextStyle | Text styling |
✏️ Example: AnimatedContainer
class MyBox extends StatefulWidget { @override _MyBoxState createState() => _MyBoxState(); } class _MyBoxState extends State<MyBox> { bool _big = false; @override Widget build(BuildContext context) { return Center( child: GestureDetector( onTap: () { setState(() { _big = !_big; }); }, child: AnimatedContainer( duration: Duration(seconds: 1), width: _big ? 200 : 100, height: _big ? 200 : 100, color: _big ? Colors.blue : Colors.red, curve: Curves.easeInOut, ), ), ); } }
➡️ Tapping the box smoothly changes its size and color.
πΉ 2. Explicit Animations
✅ What Are They?
Explicit animations give you full control over the animation, including:
Start and stop
Duration and curve
Repeating and reversing
You use AnimationController, Tween, and AnimationBuilder.
π§ Key Components
Part | Purpose |
---|---|
AnimationController | Controls the animation time |
Tween | Defines the value range |
Animation | Connects controller to widget |
AnimatedBuilder | Rebuilds UI with animation changes |
✏️ Example: Using AnimationController
class MyAnimation extends StatefulWidget { @override _MyAnimationState createState() => _MyAnimationState(); } class _MyAnimationState extends State<MyAnimation> with SingleTickerProviderStateMixin { late AnimationController _controller; late Animation<double> _animation; @override void initState() { super.initState(); _controller = AnimationController( vsync: this, duration: Duration(seconds: 2), ); _animation = Tween<double>(begin: 0, end: 300).animate(_controller); _controller.forward(); } @override Widget build(BuildContext context) { return AnimatedBuilder( animation: _animation, builder: (context, child) { return Container( width: _animation.value, height: 100, color: Colors.green, ); }, ); } @override void dispose() { _controller.dispose(); super.dispose(); } }
➡️ The box grows from 0 to 300 pixels smoothly over 2 seconds.
π Implicit vs Explicit: Comparison
Feature | Implicit Animation | Explicit Animation |
---|---|---|
Setup | Very easy | More setup required |
Control | Limited | Full control (start, stop, etc.) |
Use Case | Simple UI transitions | Complex, custom animations |
Common Widgets | AnimatedContainer , AnimatedOpacity | AnimationController , Tween |
π Bonus: AnimatedSwitcher (Implicit + Magic!)
Use AnimatedSwitcher
to switch between widgets with animation:
AnimatedSwitcher( duration: Duration(milliseconds: 500), child: _isOn ? Icon(Icons.lightbulb, key: ValueKey(1)) : Icon(Icons.lightbulb_outline, key: ValueKey(2)), )
➡️ Smoothly switches icons with a fade or scale effect.
π― When to Use What?
You Want To… | Use This Type |
---|---|
Animate simple size or color change | Implicit |
Create a custom animation sequence | Explicit |
Control timing and direction | Explicit |
Add basic fade/slide | Implicit or Switcher |
π§ Tips for Flutter Animations
Use Curves like
Curves.easeInOut
for natural movementAlways dispose of your
AnimationController
Use
TickerProviderStateMixin
for animations inStatefulWidget
Test performance on real devices (animations may lag in emulators)
Start small — build step by step
π Useful Widgets for Animation
Widget | Description |
---|---|
AnimatedCrossFade | Fades between two child widgets |
FadeTransition | Fade using controller |
ScaleTransition | Scale in/out |
SlideTransition | Move widget across screen |
Hero | Animate between screens |
π§ Summary
Term | Meaning |
---|---|
Implicit Animation | Auto animation on property change |
Explicit Animation | Full control with controller and tween |
Tween | Defines the value range |
Controller | Manages animation flow |
AnimatedBuilder | Rebuilds UI based on animation value |
- Get link
- X
- Other Apps
Comments
Post a Comment