Flutter AnimatedBuilder Widget (original) (raw)

Last Updated : 23 Jul, 2025

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 animated using widgets like AnimatedContainer or AnimatedOpacity.In this article, we are going to implement the AnimatedBuilder widget. A sample video is given below to get an idea about what we are going to do in this article.

Basic Syntax of AnimatedBuilder Widget:

Dart `

AnimatedBuilder( animation: animation, // The animation that triggers rebuilds builder: (BuildContext context, Widget? child) { return YourAnimatedWidget(); // The widget you want to animate }, )

`

Required Tools

To build this app, you need the following items installed on your machine:

Step By Step Implementations

Step 1: Create a New Project in Android Studio

To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to **Creating a Simple Application in Flutter.

Step 2: Import the Package

First of all import material.dart file.

import 'package:flutter/material.dart';

Step 3: Execute the main Method

Here the execution of our app starts.

Dart `

void main() { runApp(MyApp()); }

`

Step 4: Create MyApp Class

In this class we are going to implement the MaterialApp , here we are also set the Theme of our App.

Dart `

class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( theme: ThemeData( // Set the app's primary theme color primarySwatch: Colors.green, ), debugShowCheckedModeBanner: false, home: AnimatedBuilderExample(), ); } }

`

Step 5: Create AnimatedSwitcherExample Class

In this class we are going to Implement the AnimatedBuilder widget that help to create a transition of a container of width and height of 50.0 to 200.0. Comments are added for better understanding.

AnimatedBuilder(
animation: _controller,
builder: (BuildContext context, Widget? child) {
return Container(
width: _animation.value, // Animate the width
height: _animation.value, // Animate the height
color: Colors.green, // Container background color
child: Center(
child: Text(
'Hello',
style: TextStyle(color: Colors.white), // Text color
),
),
);
},
),

Dart `

class AnimatedBuilderExample extends StatefulWidget { @override _AnimatedBuilderExampleState createState() => _AnimatedBuilderExampleState(); }

class _AnimatedBuilderExampleState extends State with SingleTickerProviderStateMixin { late AnimationController _controller; late Animation _animation;

@override void initState() { super.initState(); _controller = AnimationController( duration: Duration(seconds: 2), // Animation duration vsync: this, ); // Tween animation _animation = Tween(begin: 50.0, end: 200.0).animate(_controller); _controller.forward(); // Start the animation }

@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('AnimatedBuilder Demo'), // App bar title ), body: Center( child: AnimatedBuilder( animation: _controller, builder: (BuildContext context, Widget? child) { return Container( width: _animation.value, // Animate the width height: _animation.value, // Animate the height color: Colors.green, // Container background color child: Center( child: Text( 'Hello', style: TextStyle(color: Colors.white), // Text color ), ), ); }, ), ), ); }

@override void dispose() { _controller.dispose(); // Dispose of the animation controller super.dispose(); } }

`

**Here is the full Code of main.dart file

Dart `

import 'package:flutter/material.dart';

void main() { runApp(MyApp()); }

class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( theme: ThemeData( primarySwatch: Colors.green, // Set the app's primary theme color ), debugShowCheckedModeBanner: false, home: AnimatedBuilderExample(), ); } }

class AnimatedBuilderExample extends StatefulWidget { @override _AnimatedBuilderExampleState createState() => _AnimatedBuilderExampleState(); }

class _AnimatedBuilderExampleState extends State with SingleTickerProviderStateMixin { late AnimationController _controller; late Animation _animation;

@override void initState() { super.initState(); _controller = AnimationController( duration: Duration(seconds: 2), // Animation duration vsync: this, ); _animation = Tween(begin: 50.0, end: 200.0).animate(_controller); // Tween animation _controller.forward(); // Start the animation }

@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('AnimatedBuilder Demo'), // App bar title ), body: Center( child: AnimatedBuilder( animation: _controller, builder: (BuildContext context, Widget? child) { return Container( width: _animation.value, // Animate the width height: _animation.value, // Animate the height color: Colors.green, // Container background color child: Center( child: Text( 'Hello', style: TextStyle(color: Colors.white), // Text color ), ), ); }, ), ), ); }

@override void dispose() { _controller.dispose(); // Dispose of the animation controller super.dispose(); } }

`

Output: