Animated Widgets in Flutter
Last Updated :
20 Feb, 2022
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("GeeksForGeeks"),
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:
Similar Reads
Animated Text in Flutter Animations make the UI more interactive and enhance the user experience. There is no limitation of creativity when it comes to animations or making the application more interactive. In Flutter, we got an easy way to display Animated text. In this article, we will see how we can animate texts using a
3 min read
Flutter - AnimatedIcon Widget Animation has become a vital part of UI design, whether it be a transition from one window to another or the closing of the window, the animation plays an important role in the smooth representation of the process. Just like that when we click on the icon, it shows animation, or another icon is show
3 min read
Flutter - AnimatedBuilder Widget The AnimatedBuilder widget in Flutter is a powerful utility widget that is used for creating complex animations by rebuilding a part of the widget tree in response to changes in an animation's value. It is especially useful when you want to animate properties of child widgets that cannot be directly
4 min read
Flutter - Loading Animation Widget In every mobile application, there is a loading animation with different colors and styles, basically, we use the loading animation when we are waiting for something. Like if we are fetching the data from the database then we have to wait for some time until the data is not fetched. So in this durat
3 min read
Flutter - AnimatedSwitcher Widget The AnimatedSwitcher widget in Flutter is used to animate the transition between two or more widgets with a smooth animation. It's often used when you want to switch the display of different widgets within the same space and provide a visual transition effect between them. In this article, we are go
4 min read
Flutter - AnimatedContainer Widget In Flutter a container is a simple widget with well-defined properties like height, width, and color, etc. The AnimatedContainer widget is a simple container widget with animations. These types of widgets can be animated by altering the values of their properties which are the same as the Container
3 min read