Animated Widgets in Flutter
The animations are considered hard work and take time to learn. Flutter made it easy with its packages. To animate the widgets without much effort, we can wrap them inside different defined animated widgets in the animate_do package. In this article, we will see how with the animate_do package we can animate widgets easily and enhance the user experience.
Add the dependency
In the pubspec.yaml file, add animate_do dependency under dependencies section. Run pub get to install this dependency or hit Ctrl + S in windows do to so.
Import the dependency
In main.dart, import the dependency in the following way:
import 'package:animate_do/animate_do.dart';
Implementation
In the animate_do package, there are different animated widgets available that we can make use of. Some of them are –
- FadeIn Animations
- FadeOut Animations
- BounceIn Animations
- ElasticIn Animations
- SlideIns Animations
- FlipIn Animations
The properties of all these animated widgets are the same. The properties are –
Dart
dynamic key, required Widget child, Duration duration = const Duration(milliseconds: 800), Duration delay = const Duration(milliseconds: 0), dynamic Function(AnimationController)? controller, bool manualTrigger = false , bool animate = true , double from = 100, |
Before moving to the animated widgets, let’s create a common widget that we will pass to each animated widget as a child. Here, we are creating a StatelessWidget NewContainer that returns a green color container.
Dart
class NewContainer extends StatelessWidget { Widget build(BuildContext context) { return Container(height: 60, width: 60, color: Colors.green); } } |
Let’s dive into examples of different animations now.
Bouncing Animation
The BounceIn animated widget involves the bouncing of the child widget. The bouncing widget can be bounced Down, Up, Left or right. To bounce down widget use BounceInDown, for Up use BounceInUp. Similarly for other directions.
Dart
BounceInUp(child: NewContainer(), duration: Duration(seconds: 4)), BounceInDown(child: NewContainer(), duration: Duration(seconds: 4)), BounceInUp(child: NewContainer(), duration: Duration(seconds: 4)), BounceInDown(child: NewContainer(), duration: Duration(seconds: 4)) |
Output:
Fading Animations
The FadeIn animated widget also involves fading animated widgets in four directions – Up, Down, Left, Right. In the below example, we are creating simple fading animations which fade in four different directions.
Dart
FadeInLeft(duration: Duration(seconds: 4), child: NewContainer()), FadeInUp(duration: Duration(seconds: 4), child: NewContainer()), FadeInDown(duration: Duration(seconds: 4), child: NewContainer()), FadeInRight(duration: Duration(seconds: 4), child: NewContainer()), |
Output:
Sliding Animations
Sliding animations can also be done in four directions – Up, Right, Down, or Left. In the below example, we are creating left sliding animations only. We are delaying the animation of each animated widget to appear in a sequence on a screen. For better visualization, see the output.
Dart
SlideInLeft( delay: Duration(seconds: 5), duration: Duration(seconds: 4), child: NewContainer()), SlideInLeft( delay: Duration(seconds: 4), duration: Duration(seconds: 4), child: NewContainer()), SlideInLeft( delay: Duration(seconds: 3), duration: Duration(seconds: 4), child: NewContainer()), SlideInLeft(duration: Duration(seconds: 4), child: NewContainer()) |
Output:
Attention Seeker Animations
There are some more animated widgets available like swing, spin, dancing widget, etc. If we want the animation to be forever, we set the infinite property of the animated widget to true.
Dart
Swing(child: NewContainer(),infinite: true ), Bounce(child: NewContainer(), infinite: true ), Dance(child: NewContainer(), infinite: true ), Roulette(child: NewContainer(), infinite: true ), Spin(child: NewContainer(), infinite: true ) |
Output:
Complete Example Code
Dart
import 'package:flutter/material.dart' ; import 'package:animate_do/animate_do.dart' ; void main() { runApp( const MyApp()); } class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return MaterialApp( title: 'Animated Widgets' , theme: ThemeData( primarySwatch: Colors.green, ), home: MyHomePage(), ); } } class MyHomePage extends StatefulWidget { @override State<MyHomePage> createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text( "w3wiki" ), centerTitle: true ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ FadeOutLeft(duration: Duration(seconds: 4), child: NewContainer()), BounceInUp(child: NewContainer(), duration: Duration(seconds: 4)), Swing( child: NewContainer(), infinite: true , ), Bounce(child: NewContainer(), infinite: true ), Dance(child: NewContainer(), infinite: true ), Roulette(child: NewContainer(), infinite: true ), Spin(child: NewContainer(), infinite: true ), SlideInLeft(duration: Duration(seconds: 4), child: NewContainer()) ], ), ), ); } } class NewContainer extends StatelessWidget { Widget build(BuildContext context) { return Container(height: 60, width: 60, color: Colors.green); } } |
Output:
Contact Us