import 'package:flutter/material.dart'; enum AnimationEffect { fade, slide, scale } class StaggeredAnimationWrapper extends StatefulWidget { final Widget child; final Duration duration; final AnimationEffect effect; final Curve curve; const StaggeredAnimationWrapper({ required this.child, this.duration = const Duration(seconds: 1), this.effect = AnimationEffect.fade, this.curve = Curves.easeOut, super.key, }); @override _StaggeredAnimationWrapperState createState() => _StaggeredAnimationWrapperState(); } class _StaggeredAnimationWrapperState extends State with SingleTickerProviderStateMixin { late final AnimationController _controller; late final Animation _animation; @override void initState() { super.initState(); _controller = AnimationController( vsync: this, duration: widget.duration, )..forward(); _animation = CurvedAnimation( parent: _controller, curve: widget.curve, ); } @override void didUpdateWidget(covariant StaggeredAnimationWrapper oldWidget) { super.didUpdateWidget(oldWidget); if (widget.child != oldWidget.child) { // Yeni widget geldiğinde ters animasyon oynat ve yenisini başlat _controller.reverse().then((_) { if (mounted) { _controller.forward(); } }); } } @override void dispose() { _controller.dispose(); super.dispose(); } Future reverseAnimation() async { await _controller.reverse(); } @override Widget build(BuildContext context) { return StaggeredChildAnimator( animation: _animation, effect: widget.effect, child: widget.child, ); } } class StaggeredChildAnimator extends StatelessWidget { final Animation animation; final Widget child; final AnimationEffect effect; const StaggeredChildAnimator({ required this.animation, required this.child, required this.effect, super.key, }); @override Widget build(BuildContext context) { return AnimatedBuilder( animation: animation, builder: (context, _) { return _buildStaggeredAnimation(child, animation); }, ); } Widget _buildStaggeredAnimation(Widget widget, Animation animation) { if (widget is! Column && widget is! Row) { return _applyEffect(widget, animation); } final children = widget is Column ? (widget).children : (widget as Row).children; return Column( children: List.generate(children.length, (index) { final intervalStart = index * 0.1; final intervalEnd = intervalStart + 0.2; final staggeredAnimation = CurvedAnimation( parent: animation, curve: Interval( intervalStart, intervalEnd, curve: Curves.easeOut, ), ); return _buildStaggeredAnimation(children[index], staggeredAnimation); }), ); } Widget _applyEffect(Widget widget, Animation animation) { switch (effect) { case AnimationEffect.fade: return Opacity( opacity: animation.value, child: widget, ); case AnimationEffect.slide: return Transform.translate( offset: Offset(0, 50 * (1 - animation.value)), child: widget, ); case AnimationEffect.scale: return Transform.scale( scale: animation.value, child: widget, ); default: return widget; } } }