Flutter - Animated ScrollView
Last Updated :
24 Apr, 2025
Creating an animated scroll view in Flutter involves using a combination of widgets and animation controllers to achieve the desired scrolling effect. Here we use Tween Animation to implement animation. In this article we are going to add an animation effect to a ScrollView A sample video is given below to get an idea about what we are going to do in this article.
Basic Syntax of Creating an Animation in Flutter
// Create an AnimationController with a 4-second duration
_animationController = AnimationController(
vsync: this, // Associate the AnimationController with the widget's lifecycle
duration: Duration(seconds: 4),
);
// Create a linear Tween animation from 0 to 1
_animation = Tween<double>(begin: 0, end: 1).animate(_animationController);
// Trigger the animation when the widget is first built
_animationController.forward();
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(
// Define the app's theme
theme: ThemeData(
primarySwatch: Colors.green, // Set the app's primary theme color
),
debugShowCheckedModeBanner: false,
home: AnimatedScrollViewDemo(),
);
}
}
Step 5: Create AnimatedScrollViewDemo Class
In this class we are going to create a ListView then add some animation to the Scrolling effect. Comments are added for better understanding.
Dart
class AnimatedScrollViewDemo extends StatefulWidget {
@override
_AnimatedScrollViewDemoState createState() => _AnimatedScrollViewDemoState();
}
// Create the main state class
class _AnimatedScrollViewDemoState extends State<AnimatedScrollViewDemo> {
// Create a list of 50 items with "Item 0", "Item 1", etc.
final List<String> items = List.generate(50, (index) => 'Item $index');
// Create a ScrollController to
// manage the ListView's scroll position
ScrollController _controller = ScrollController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Animated Scroll View Demo'),
),
body: ListView.builder(
controller:
_controller, // Associate the ScrollController with the ListView
itemCount: items.length,
itemBuilder: (context, index) {
return AnimatedItemWidget(
index: index,
item: items[index],
);
},
),
);
}
}
Step 6: Create AnimatedItemWidget Class
In this class we going to Create a widget to represent an individual item with animations. Comments are added for better understanding of the code.
Dart
// Create a widget to represent an individual
// item with animations
class AnimatedItemWidget extends StatefulWidget {
final int index;
final String item;
AnimatedItemWidget({required this.index, required this.item});
@override
_AnimatedItemWidgetState createState() => _AnimatedItemWidgetState();
}
// Create the state for an individual item
class _AnimatedItemWidgetState extends State<AnimatedItemWidget>
with SingleTickerProviderStateMixin {
// Define an AnimationController for controlling animations
late AnimationController _animationController;
// Define an Animation to interpolate
// values between 0 and 1
late Animation<double> _animation;
@override
void initState() {
super.initState();
// Create an AnimationController with a 4-second duration
_animationController = AnimationController(
vsync:
this, // Associate the AnimationController
// with the widget's lifecycle
duration: Duration(seconds: 4),
);
// Create a linear Tween animation from 0 to 1
_animation = Tween<double>(begin: 0, end: 1).animate(_animationController);
// Trigger the animation when the widget is first built
_animationController.forward();
}
@override
Widget build(BuildContext context) {
return FadeTransition(
opacity: _animation,
child: Card(
elevation: 2,
margin: EdgeInsets.all(8),
child: ListTile(
title: Text(widget.item),
),
),
);
}
@override
void dispose() {
// Dispose of the AnimationController
// to free resources
_animationController.dispose();
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(
// Define the app's theme
theme: ThemeData(
// Set the app's primary theme color
primarySwatch: Colors.green,
),
debugShowCheckedModeBanner: false,
home: AnimatedScrollViewDemo(),
);
}
}
class AnimatedScrollViewDemo extends StatefulWidget {
@override
_AnimatedScrollViewDemoState createState() => _AnimatedScrollViewDemoState();
}
// Create the main state class
class _AnimatedScrollViewDemoState extends State<AnimatedScrollViewDemo> {
// Create a list of 50 items with "Item 0", "Item 1", etc.
final List<String> items = List.generate(50, (index) => 'Item $index');
// Create a ScrollController to manage the ListView's scroll position
ScrollController _controller = ScrollController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Animated Scroll View Demo'),
),
body: ListView.builder(
controller:
_controller, // Associate the ScrollController with the ListView
itemCount: items.length,
itemBuilder: (context, index) {
return AnimatedItemWidget(
index: index,
item: items[index],
);
},
),
);
}
}
// Create a widget to represent an individual item with animations
class AnimatedItemWidget extends StatefulWidget {
final int index;
final String item;
AnimatedItemWidget({required this.index, required this.item});
@override
_AnimatedItemWidgetState createState() => _AnimatedItemWidgetState();
}
// Create the state for an individual item
class _AnimatedItemWidgetState extends State<AnimatedItemWidget>
with SingleTickerProviderStateMixin {
// Define an AnimationController for controlling animations
late AnimationController _animationController;
// Define an Animation to interpolate values between 0 and 1
late Animation<double> _animation;
@override
void initState() {
super.initState();
// Create an AnimationController with a 4-second duration
_animationController = AnimationController(
vsync:
this, // Associate the AnimationController with the widget's lifecycle
duration: Duration(seconds: 4),
);
// Create a linear Tween animation from 0 to 1
_animation = Tween<double>(begin: 0, end: 1).animate(_animationController);
// Trigger the animation when the widget is first built
_animationController.forward();
}
@override
Widget build(BuildContext context) {
return FadeTransition(
opacity: _animation,
child: Card(
elevation: 2,
margin: EdgeInsets.all(8),
child: ListTile(
title: Text(widget.item),
),
),
);
}
@override
void dispose() {
// Dispose of the AnimationController to free resources
_animationController.dispose();
super.dispose();
}
}
Output:
Similar Reads
Flutter - Custom Scroll View
A CustomScrollView in Flutter is a highly customizable scrolling widget that allows you to create complex scrolling effects and layouts. You can use it to create scrollable views with multiple slivers, each having its own behavior. In this article, we are going to implement the CustomScrollView widg
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 - Animated Splash Screen
The Animated Splash screen is used for a startup screen in a Flutter application. More or less all applications use them generally to show the logo of the institution and its creators awareness. This although holds no functionality but it can be great to increase product awareness and promotion.Let'
3 min read
Flutter - Movie Tween Animation
Basically in Movie Tween Animation, we define different scenes, each with its own set of tweens and then Animate them. In Flutter we can achieve this type of animation easily by using the simple_animations package. In this article, we are going to implement the movie tween animation with the help of
6 min read
Flutter - Lottie Animation
Visualization is an integral part of any application. Animations can highly glorify the UI of an app, but animations can be hectic to implement for an application. This is where the Lottie animation comes in. Lottie is a JSON-based animation file. It can be used both as a network asset and a static
3 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
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 - AnimatedIcon Widget
Animation has become a vital part of UI design, whether it be a transition from one window to another or the closing of the window, the animation plays an important role in the smooth representation of the process. Just like that when we click on the icon, it shows animation, or another icon is show
3 min read
Flutter - Wave Animation
In this article, we are going to make a Flutter application that demonstrates the creation of a dynamic wave animation. This animation simulates the motion of water waves, creating an engaging visual effect that can be used in various Flutter applications to add styling effects. A sample video is gi
5 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