Flutter - AnimatedContainer Widget
Last Updated :
20 Dec, 2022
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 widget. These types of animation in Flutter is known as 'Implicit Animation. We will discuss then in detail in this article by building a simple app with AnimatedContainer widget.
Constructor of AnimatedContainer class:
AnimatedContainer(
{Key key,
AlignmentGeometry alignment,
EdgeInsetsGeometry padding,
Color color,
Decoration decoration,
Decoration foregroundDecoration,
double width,
double height,
BoxConstraints constraints,
EdgeInsetsGeometry margin,
Matrix4 transform,
Widget child,
Curve curve: Curves.linear,
@required Duration duration,
VoidCallback onEnd}
)
Properties of AnimatedContainer Widget:
- alignment: This property takes AlignmentGeometry class as the object. It controls the alignment of the child widget with the container.
- child: This property holds a widget as the object to show inside the AnimatedContainer.
- constraints: BoxConstraints class is the object to this property. It applies some extra constraints to the child widget in the AnimatedContainer.
- decoration: This property takes in Decoration class as the object to apply color behind the child widget.
- foregroundDecoration: This property controls the default color of the text inside the AnimatedContainer.
- margin: The margin property holds EdgeInsetsGeometry class as the object. It adds empty space around the widget.
- padding: This property also takes EdgeInsetsGeometry class as the object to add empty space inside the AnimatedContainer and the child widget.
- transform: This property takes Matrix4 as the object to apply matrix transformation before painting the AnimatedContainer.
Follow the below steps to build an application with AnimatedContainer widget:
- Create a StatefulWidget and define its properties.
- Add an AnimatedContainer widget and define its properties.
- Create animation by altering those properties.
Let's discuss them in detail.
Creating a StatefulWidget:
Use the custom State class to create a StatefulWidget and define its properties as shown below:
Dart
class AnimatedContainerApp extends StatefulWidget {
@override
_AnimatedContainerAppState createState() => _AnimatedContainerAppState();
}
class _AnimatedContainerAppState extends State<AnimatedContainerApp> {
double _width = 55;
double _height = 55;
Color _color = Colors.green;
BorderRadiusGeometry _borderRadius = BorderRadius.circular(9);
@override
Widget build(BuildContext context) {
}
}
Adding AnimatedContainer widget:
Add an AnimatedContainer widget with its duration property defined that determines how long the container is going to animate as shown below:
Dart
AnimatedContainer(
width: _width,
height: _height,
decoration: BoxDecoration(
color: _color,
borderRadius: _borderRadius,
),
duration: Duration(seconds: 1),
curve: Curves.fastOutSlowIn,
);
Altering the properties:
Rebuilding and changing the properties after the end of duration specified property is done as shown below:
Dart
FloatingActionButton(
child: Icon(Icons.play_arrow),
onPressed: () {
setState(() {
final random = Random();
// random dimension generator
_width = random.nextInt(300).toDouble();
_height = random.nextInt(300).toDouble();
// random color generator
_color = Color.fromRGBO(
random.nextInt(256),
random.nextInt(256),
random.nextInt(256),
1,
);
_borderRadius =
BorderRadius.circular(random.nextInt(100).toDouble());
});
},
);
Complete Source Code:
Dart
import 'dart:math';
import 'package:flutter/material.dart';
void main() => runApp(AnimatedContainerApp());
class AnimatedContainerApp extends StatefulWidget {
@override
_AnimatedContainerAppState createState() => _AnimatedContainerAppState();
}
class _AnimatedContainerAppState extends State<AnimatedContainerApp> {
double _width = 70;
double _height = 70;
Color _color = Colors.green;
BorderRadiusGeometry _borderRadius = BorderRadius.circular(10);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('GeeksForGeeks'),
backgroundColor: Colors.green,
),
body: Center(
child: AnimatedContainer(
width: _width,
height: _height,
decoration: BoxDecoration(
color: _color,
borderRadius: _borderRadius,
),
duration: Duration(seconds: 1),
curve: Curves.fastOutSlowIn,
),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.play_arrow),
backgroundColor: Colors.green,
onPressed: () {
setState(() {
// random generator
final random = Random();
// random dimension generator
_width = random.nextInt(500).toDouble();
_height = random.nextInt(500).toDouble();
// random color generator
_color = Color.fromRGBO(
random.nextInt(300),
random.nextInt(300),
random.nextInt(300),
1,
);
// random radius generator
_borderRadius =
BorderRadius.circular(random.nextInt(100).toDouble());
});
},
),
),
);
}
}
Output:
Similar Reads
Flutter Tutorial This Flutter Tutorial is specifically designed for beginners and experienced professionals. It covers both the basics and advanced concepts of the Flutter framework.Flutter is Googleâs mobile SDK that builds native Android and iOS apps from a single codebase. It was developed in December 2017. When
7 min read
Top 50 Flutter Interview Questions and Answers for 2025 Flutter is an open-source, cross-platform application development framework. It was developed by Google in 2017. It is used to build applications for Android, iOS, Linux, Mac, Windows, and the web. Flutter uses the Dart programming language. It provides a simple, powerful, efficient, and easy-to-und
15+ min read
What is Widgets in Flutter? Flutter is Google's UI toolkit for crafting beautiful, natively compiled iOS and Android apps from a single code base. To build any application we start with widgets - The building block of Flutter applications. Widgets describe what their view should look like given their current configuration and
5 min read
Flutter | An introduction to the open source SDK by Google Flutter is Googleâs Mobile SDK to build native iOS and Android, Desktop (Windows, Linux, macOS), and Web apps from a single codebase. When building applications with Flutter, everything is Widgets â the blocks with which the flutter apps are built. They are structural elements that ship with a bunch
5 min read
Android Studio Setup for Flutter Development This article will show how to set up Android Studio to run Flutter Applications. Android Studio is one of the popular IDE( integrated development environment  ) developed by Google itself to create cross-platform Android applications. First, you have to install Android Studio version 3.0 or later, a
3 min read
10 Best Flutter Projects with Source Code in 2025 Are you eager to begin your journey into Flutter app development but find yourself unsure of where to start? Look no further! This article serves as a comprehensive guide for aspiring developers, offering a wide range of innovative Flutter project ideas. Whether you're looking to refine your skills
7 min read
Flutter - Architecture Application Flutter architecture application mainly consists of: WidgetsGesturesConcept of StateLayersWidgetsWidgets are the primary component of any flutter application. It acts as a UI for the user to interact with the application. Any flutter application is itself a widget that is made up of a combination of
3 min read
Flutter - Changing App Icon Flutter SDK is an open-source software development kit for building beautiful UI which is natively compiled. When we create a Flutter Project, it comes with the default Flutter icon. In order to get the app published in stores like Google Play Store, Apple App Store, etc the default icon can be chan
3 min read
Flutter - AppBar Widget AppBar is usually the topmost component of the app (or sometimes the bottom-most), it contains the toolbar and some other common action buttons. As all the components in a Flutter application are a widget or a combination of widgets. So AppBar is also a built-in class or widget in Flutter which give
7 min read
Scaffold class in Flutter with Examples The Scaffold is a class in flutter that provides many widgets or we can say APIs. The Scaffold will expand or occupy the whole available space in device screen .The class Hierarchy is as follows:Object â³ Diagnosticable â³ Diagnosticable Tree â³ Widget â³ StateFul Widget â³ ScaffoldThe constructor of the
8 min read