Flutter AnimatedContainer and AnimatedOpacity – Basic Animations
March 10, 2025 | by Adesh Yadav

Flutter AnimatedContainer and AnimatedOpacity – Basic Animations
Flutter has revolutionized the way developers create beautiful, natively compiled applications for mobile, web, and desktop from a single codebase.One of the standout features within Flutter is its rich set of animation capabilities. In this article, we will explore two essential animation widgets: AnimatedContainer and AnimatedOpacity. These tools allow developers to create smooth transitions and engaging user interfaces with minimal effort.
Understanding Basic Animations in Flutter
Animations in Flutter rely on implicit animations, which are simple to implement and require less code than explicit animations. Among these,AnimatedContainer
and AnimatedOpacity
stand out as two of the easiest and most commonly used widgets for creating basic animations.
what is AnimatedContainer?
AnimatedContainer is a widget that you can use to animate changes to its properties over a period of time. When one or more properties change, an implicit animation builds a smooth transition between the old and new values.
Key Features of AnimatedContainer
- Simplicity: It requires just a few lines of code to implement simple animations.
- Automatic animation: Flutter automatically handles the animation for you based on the changes in properties.
- Customized Duration: You can specify how long the animation should take.
Basic Example of AnimatedContainer
Here’s a simple example of how to use AnimatedContainer
in a Flutter request:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State {
double _width = 100.0;
double _height = 100.0;
Color _color = Colors.blue;
void _changeProperties() {
setState(() {
_width = _width == 100.0 ? 200.0 : 100.0;
_height = _height == 100.0 ? 200.0 : 100.0;
_color = _color == Colors.blue ? Colors.red : Colors.blue;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: appbar(title: Text('AnimatedContainer Example')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
AnimatedContainer(
width: _width,
height: _height,
color: _color,
alignment: alignment.center,
duration: duration(seconds: 1),
child: Text('Tap me!', style: TextStyle(color: Colors.white)),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: _changeProperties,
child: Text('Change properties'),
),
],
),
),
),
);
}
}
What is animatedopacity?
AnimatedOpacity is another essential widget used to animate the opacity of a widget. This is perfect for fading in and out elements within your Flutter application.
Key Features of AnimatedOpacity
- Ease of Use: Similar to
AnimatedContainer
, it enables quick animations without much complexity. - Control Over Opacity: You can define how visible a widget should be during the animation.
- Perfect for Transitions: Excellent for creating visually appealing transitions during state changes.
Basic Example of AnimatedOpacity
Here is how you can use animatedopacity
in a Flutter app:
import 'package:flutter/material.dart';
void main() => runApp(MyOpacityApp());
class MyOpacityApp extends StatefulWidget {
@override
_MyOpacityAppState createState() => _MyOpacityAppState();
}
class _MyOpacityAppState extends State {
double _opacity = 1.0;
void _toggleOpacity() {
setState(() {
_opacity = _opacity == 1.0 ? 0.0 : 1.0;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('AnimatedOpacity Example')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
AnimatedOpacity(
opacity: _opacity,
duration: Duration(seconds: 1),
child: Text('Hello Flutter!', style: TextStyle(fontSize: 24)),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: _toggleOpacity,
child: Text('Toggle Opacity'),
),
],
),
),
),
);
}
}
benefits of Using AnimatedContainer and AnimatedOpacity
benefit | description |
---|---|
Improved User Experiance | Smooth transitions enhance interactivity and engagement. |
Easier Debugging | Animation frameworks make it easier to visualize states throughout your app. |
Customization | Control animation speed, curve, and duration based on your app needs. |
Performance | Implicit animations are less taxing on the performance compared to explicit animations. |
Practical Tips for Implementing Basic Animations
- Keep It Simple: Don’t overwhelm users with too many animations. Use them to enhance user experiences.
- Consistency matters: Maintain the same style of animations across your app to create a cohesive look.
- Use Duration Wisely: Experiment with animation durations to strike the right balance between smoothness and responsiveness.
- Test on Real Devices: Always test your animations on real devices to assess performance and visual effects.
Conclusion
Animations can substantially enhance the user interface of your Flutter applications, making them feel more responsive and engaging. By using the AnimatedContainer and AnimatedOpacity widgets,developers can easily create visually appealing transitions with minimal code. Remember to apply animations thoughtfully and maintain consistency throughout your applications to achieve the best user experience. Start experimenting with these widgets today to elevate your flutter apps to the next level!
RELATED POSTS
View all