How to create background animations in Flutter
Animations can add a delightful touch to your Flutter app, making it more engaging and visually appealing. Often, you might need to incorporate some background animations in your app in order to make your app look more lively and interactive. For this purpose, Flutter provides powerful animation tools that allow developers to create eye-catching background animations easily.
Flutter animations
Flutter's animation framework is based on the concept of animation controllers and animation widgets. Animation controllers are used to control the state of an animation, while animation widgets are used to display the animated content on the screen.
Here are the key components of the animation framework:
Animation controller: It's responsible for defining the animation's duration, providing the current animation value, and starting/stopping the animation.
Tween: A Tween is a mapping between a range of values for the animation's beginning and end states.
Animated widget: The animated widget rebuilds itself each time the animation value changes, allowing you to animate any property of a widget.
Creating a background animation in Flutter
Let's create a simple example of a background animation using Flutter. In this example, we will animate the background color of the screen to create a smooth color transition effect.
Implement the background animation
Next, we incorporate the background animations in our main.dart file, or we can create a separate custom widget to import and use it anywhere else.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: BackgroundAnimationScreen(),
);
}
}
class BackgroundAnimationScreen extends StatefulWidget {
@override
_BackgroundAnimationScreenState createState() => _BackgroundAnimationScreenState();
}
class _BackgroundAnimationScreenState extends State<BackgroundAnimationScreen>
with SingleTickerProviderStateMixin {
AnimationController _controller;
Animation<Color> _colorAnimation;
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: Duration(seconds: 2),
vsync: this,
);
_colorAnimation = ColorTween(
begin: Colors.blue,
end: Colors.red,
).animate(_controller);
_controller.repeat(reverse: true);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: AnimatedBuilder(
animation: _colorAnimation,
builder: (context, child) {
return Container(
color: _colorAnimation.value,
child: child,
);
},
child: Center(
child: Text(
'Educative.io',
style: TextStyle(
fontSize: 30,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
),
),
);
}
}
Code explanation
The
mainfunction is the entry point of the app. It calls therunAppmethod to start the Flutter application and passMyAppas the root widget.Lines 4–11:
MyAppis a StatelessWidget that returns a MaterialApp withBackgroundAnimationScreenas the initial route.Lines 18–22:
BackgroundAnimationScreenis a StatefulWidget. It represents the main screen of the app, where the background animation will be displayed._BackgroundAnimationScreenStateis the state class for theBackgroundAnimationScreen.SingleTickerProviderStateMixinis added to provide the animation ticker to the_controller.
Lines 24–37: In the
initStatemethod, the_controlleris initialized to control the animation. The animation duration is set to 2 seconds, andvsync: thisis provided for better performance._colorAnimationis set up using aColorTweento animate the background color from blue to red. The animation is attached to the_controller._controller.repeat(reverse: true)starts the animation, making it loop continuously between blue and red colors.
Lines 46–68: In the
buildmethod, a Scaffold is created as the root widget, representing the main app screen.The
AnimatedBuilderwidget is used to rebuild the UI whenever the_colorAnimationupdates.The
Containerserves as the background element and its color is set to the current value of_colorAnimation.The
childof theAnimatedBuilderis theCenterwidget containing aTextwidget with the text "Educative.io". This text will be displayed at the center of the screen, providing the foreground content.
Free Resources