Tutorial and code of Staggered Animations in Flutter. Copy and paste the below code as per your requirements.
flutter_staggered_animations: ^1.0.0
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_animations/flutter_staggered_animations.dart';
void main() => runApp(App());
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
brightness: Brightness.light,
scaffoldBackgroundColor: const Color.fromRGBO(239, 238, 239, 1.0),
),
home: const CardGridScreen(),
);
}
}
class CardGridScreen extends StatefulWidget {
const CardGridScreen({Key? key}) : super(key: key);
@override
_CardGridScreenState createState() => _CardGridScreenState();
}
class _CardGridScreenState extends State<CardGridScreen> {
@override
Widget build(BuildContext context) {
var columnCount = 3;
return AutoRefresh(
duration: const Duration(milliseconds: 2000),
child: Scaffold(
appBar: AppBar(title: const Center(child: Text("Staggered Animations"),),),
body: SafeArea(
child: AnimationLimiter(
child: GridView.count(
childAspectRatio: 1.0,
padding: const EdgeInsets.all(8.0),
crossAxisCount: columnCount,
children: List.generate(
100,
(int index) {
return AnimationConfiguration.staggeredGrid(
columnCount: columnCount,
position: index,
duration: const Duration(milliseconds: 375),
child: const ScaleAnimation(
scale: 0.5,
child: FadeInAnimation(
child: EmptyCard(),
),
),
);
},
),
),
),
),
),
);
}
}
class AutoRefresh extends StatefulWidget {
final Duration duration;
final Widget child;
const AutoRefresh({
Key? key,
required this.duration,
required this.child,
}) : super(key: key);
@override
_AutoRefreshState createState() => _AutoRefreshState();
}
class _AutoRefreshState extends State<AutoRefresh> {
int? keyValue;
ValueKey? key;
Timer? _timer;
@override
void initState() {
super.initState();
keyValue = 0;
key = ValueKey(keyValue);
_recursiveBuild();
}
@override
Widget build(BuildContext context) {
return Container(
key: key,
child: widget.child,
);
}
void _recursiveBuild() {
_timer = Timer(
widget.duration,
() {
setState(() {
keyValue = keyValue! + 1;
key = ValueKey(keyValue);
_recursiveBuild();
});
},
);
}
@override
void dispose() {
_timer?.cancel();
super.dispose();
}
}
class EmptyCard extends StatelessWidget {
final double? width;
final double? height;
const EmptyCard({
Key? key,
this.width,
this.height,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
width: width,
height: height,
margin: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 8.0),
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(4.0)),
boxShadow: <BoxShadow>[
BoxShadow(
color: Colors.black12,
blurRadius: 4.0,
offset: Offset(0.0, 4.0),
),
],
),
);
}
}