Flutter - Animate Items in List Using AnimatedList
Last Updated :
24 Apr, 2025
The AnimatedList widget in Flutter is used to create a list that automatically animates changes to its items. It's particularly useful when you want to add or remove items from a list with smooth animations. In this article, we are going to see how the list is animated when an item is deleted or added to the list. In this article, we are going to implement the AnimatedList widget. A sample video is given below to get an idea about what we are going to do in this article.
Basic Syntax of AnimatedList Widget
AnimatedList(
key: GlobalKey<AnimatedListState>(), // A unique key to identify the list
initialItemCount: itemCount, // The initial number of items in the list
itemBuilder: (BuildContext context, int index, Animation<double> animation) {
// The builder function for creating list items
return YourListItemWidget(item: yourDataList[index], animation: animation);
},
)
Required Tools
To build this app, you need the following items installed on your machine:
- Visual Studio Code / Android Studio
- Android Emulator / iOS Simulator / Physical Device device.
- Flutter Installed
- Flutter plugin for VS Code / Android Studio.
Step By Step Implementation
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(
primarySwatch:
Colors.green, // Set the app's primary theme color to green
),
debugShowCheckedModeBanner: false,
home: MyAnimatedList(),
);
}
}
Step 5: Create MyAnimatedList Class
In this class we are going to Implement the AnimatedList widget that help to Animated the list when an item is added or removed. This class contains 3 methods :
- _addItem method is used to add a new item to the list.
- _removeItem method is used to remove an item from the list.
- buildItem is a helper method that builds each item in the list. It uses SizeTransition for the animation effect.
Comments are added for better understanding.
AnimatedList(
key: _listKey,
initialItemCount: _items.length,
itemBuilder: (context, index, animation) {
return buildItem(_items[index], animation); // Build each list item
},
),
Dart
class MyAnimatedList extends StatefulWidget {
@override
_MyAnimatedListState createState() => _MyAnimatedListState();
}
class _MyAnimatedListState extends State<MyAnimatedList> {
final GlobalKey<AnimatedListState> _listKey = GlobalKey<AnimatedListState>();
List<String> _items = [
'Item 1',
'Item 2',
'Item 3',
'Item 4',
'Item 5',
'Item 6',
'Item 7',
'Item 8',
'Item 9',
'Item 10'
];
void _addItem() {
final newIndex = _items.length; // Calculate the index for the new item
final newItem = 'Item ${newIndex + 1}'; // Create a new item
// Update the underlying data list
setState(() {
_items.add(newItem);
});
// Insert the new item into the AnimatedList
_listKey.currentState!.insertItem(newIndex);
}
void _removeItem(int index) {
final removedItem = _items[index];
// Remove the item from the data list
setState(() {
_items.removeAt(index);
});
// Remove the item from the AnimatedList with animation
_listKey.currentState!.removeItem(
index,
(context, animation) => buildItem(removedItem, animation),
);
}
Widget buildItem(String item, Animation<double> animation) {
return SizeTransition(
sizeFactor: animation,
child: ListTile(
title: Text(item), // Display the item's text
trailing: IconButton(
icon: Icon(Icons.delete), // Display a delete icon
onPressed: () =>
_removeItem(_items.indexOf(item)), // Remove the item when pressed
),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('AnimatedList Example'), // Set the app bar title
),
body: AnimatedList(
key: _listKey,
initialItemCount: _items.length,
itemBuilder: (context, index, animation) {
return buildItem(_items[index], animation); // Build each list item
},
),
floatingActionButton: FloatingActionButton(
onPressed: _addItem, // Add a new item when the FAB is pressed
child: Icon(Icons.add), // Display a plus icon
),
);
}
}
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:
// Set the app's primary
// theme color to green
Colors.green,
),
debugShowCheckedModeBanner: false,
home: MyAnimatedList(),
);
}
}
class MyAnimatedList extends StatefulWidget {
@override
_MyAnimatedListState createState() => _MyAnimatedListState();
}
class _MyAnimatedListState extends State<MyAnimatedList> {
final GlobalKey<AnimatedListState> _listKey = GlobalKey<AnimatedListState>();
List<String> _items = [
'Item 1',
'Item 2',
'Item 3',
'Item 4',
'Item 5',
'Item 6',
'Item 7',
'Item 8',
'Item 9',
'Item 10'
];
void _addItem() {
final newIndex = _items.length; // Calculate the index for the new item
final newItem = 'Item ${newIndex + 1}'; // Create a new item
// Update the underlying data list
setState(() {
_items.add(newItem);
});
// Insert the new item into the AnimatedList
_listKey.currentState!.insertItem(newIndex);
}
void _removeItem(int index) {
final removedItem = _items[index];
// Remove the item from the data list
setState(() {
_items.removeAt(index);
});
// Remove the item from the AnimatedList with animation
_listKey.currentState!.removeItem(
index,
(context, animation) => buildItem(removedItem, animation),
);
}
Widget buildItem(String item, Animation<double> animation) {
return SizeTransition(
sizeFactor: animation,
child: ListTile(
title: Text(item), // Display the item's text
trailing: IconButton(
icon: Icon(Icons.delete), // Display a delete icon
onPressed: () =>
_removeItem(_items.indexOf(item)), // Remove the item when pressed
),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('AnimatedList Example'), // Set the app bar title
),
body: AnimatedList(
key: _listKey,
initialItemCount: _items.length,
itemBuilder: (context, index, animation) {
return buildItem(_items[index], animation); // Build each list item
},
),
floatingActionButton: FloatingActionButton(
onPressed: _addItem, // Add a new item when the FAB is pressed
child: Icon(Icons.add), // Display a plus icon
),
);
}
}
Output:
Similar Reads
Animated Widgets in Flutter
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 ca
4 min read
Flutter - Create Animation Using the AnimatedAlign Widget
The AnimatedAlign widget in Flutter is used to create animated transitions for aligning a child widget within a parent container. It smoothly animates the alignment property, allowing you to move a child widget from one position to another with a specified duration. In this article, we are going to
4 min read
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 - Animation in Route Transition
Routes are simply Pages in Flutter applications. An application often needs to move from one page to another. However, animations can be used to make these transitions smoother. These animations can be used to curve or tween the Animation object of the PageRouteBuilder class to alter the transition
4 min read
Flutter - Animated AlertDialog in Flutter
Animating an AlertDialog in Flutter involves using the Flutter animations framework to create custom animations for showing and hiding the dialogue. In this article, we are going to add an animation to an AlertDialog. A sample video is given below to get an idea about what we are going to do in this
4 min read
Flutter - Physics Simulation in Animation
Physics simulation in Flutter is a beautiful way to Animate components of the flutter app to make it look more realistic and interactive. These can be used to create a range of animations like falling objects due to gravity to making a container seem attached to a spring. In this article, we will ex
4 min read
Flutter - Filter a List Using Some Condition
In Flutter, the where() method can be used on a list to filter its elements based on a specified condition. To demonstrate this let's make a Flutter app that will contain a List of products having name, category and price. Then we create a TextView named a Filtered list by price when the user enters
6 min read
AnimatedList Flutter
AnimatedList in Flutter is a dynamic list component that enables us to insert and remove elements dynamically. AnimatedList is a list that animates the items when they are removed or inserted enhancing the user experience. As we all using ListView in Flutter lists for displaying a collection of item
8 min read
Rive animations in Flutter
Rive is a very useful animation tool that can create beautiful animations and we can add these in our Application. In flutter, we can add animations by writing so many lines of code but this is not a good practice for a developer. Instead of writing lines of code to create animation, we can create o
2 min read
Flutter - Animated Navigation
By default, Flutter has no animation when navigating from one Screen to another Screen. But in this tutorial, we are going to learn how to animate the Screen Transition. Project Setup Before directly applying to your existing project, practice the code in some example projects. For this tutorial, we
4 min read