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.
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.
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.
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.
// 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
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();
}
} |