import 'package:flutter/material.dart'; import 'dart:math' as math; class CelebrationOverlay extends StatefulWidget { final Widget child; final bool showCelebration; final VoidCallback? onCelebrationComplete; const CelebrationOverlay({ super.key, required this.child, this.showCelebration = false, this.onCelebrationComplete, }); @override State createState() => _CelebrationOverlayState(); } class _CelebrationOverlayState extends State with TickerProviderStateMixin { late AnimationController _controller; late Animation _fadeAnimation; late Animation _scaleAnimation; late List _particles; @override void initState() { super.initState(); _controller = AnimationController( duration: const Duration(milliseconds: 2500), vsync: this, ); _fadeAnimation = Tween( begin: 0.0, end: 1.0, ).animate(CurvedAnimation( parent: _controller, curve: const Interval(0.0, 0.3, curve: Curves.easeOut), )); _scaleAnimation = Tween( begin: 0.0, end: 1.0, ).animate(CurvedAnimation( parent: _controller, curve: const Interval(0.0, 0.6, curve: Curves.elasticOut), )); _particles = _generateParticles(); _controller.addStatusListener((status) { if (status == AnimationStatus.completed) { widget.onCelebrationComplete?.call(); } }); } @override void didUpdateWidget(CelebrationOverlay oldWidget) { super.didUpdateWidget(oldWidget); if (widget.showCelebration && !oldWidget.showCelebration) { _startCelebration(); } } void _startCelebration() { _controller.reset(); _particles = _generateParticles(); _controller.forward(); } List _generateParticles() { final random = math.Random(); return List.generate(30, (index) { return Particle( x: random.nextDouble(), y: random.nextDouble(), color: _getRandomColor(random), size: 3.0 + random.nextDouble() * 5.0, speedX: (random.nextDouble() - 0.5) * 2.0, speedY: random.nextDouble() * -2.0 - 1.0, ); }); } Color _getRandomColor(math.Random random) { final colors = [ const Color(0xFFFFD700), // Gold const Color(0xFFFF6B35), // Orange const Color(0xFF4ECDC4), // Teal const Color(0xFF45B7D1), // Blue const Color(0xFF96CEB4), // Green const Color(0xFFFECEA8), // Peach const Color(0xFFFF9FF3), // Pink const Color(0xFFF9CA24), // Yellow ]; return colors[random.nextInt(colors.length)]; } @override void dispose() { _controller.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Stack( children: [ widget.child, if (widget.showCelebration) AnimatedBuilder( animation: _controller, builder: (context, child) { return Positioned.fill( child: Opacity( opacity: _fadeAnimation.value, child: CustomPaint( painter: CelebrationPainter( particles: _particles, progress: _controller.value, scale: _scaleAnimation.value, ), size: Size.infinite, ), ), ); }, ), ], ); } } class Particle { final double x; final double y; final Color color; final double size; final double speedX; final double speedY; Particle({ required this.x, required this.y, required this.color, required this.size, required this.speedX, required this.speedY, }); } class CelebrationPainter extends CustomPainter { final List particles; final double progress; final double scale; CelebrationPainter({ required this.particles, required this.progress, required this.scale, }); @override void paint(Canvas canvas, Size size) { final paint = Paint()..style = PaintingStyle.fill; for (final particle in particles) { final currentX = particle.x * size.width + particle.speedX * progress * size.width * 0.5; final currentY = particle.y * size.height + particle.speedY * progress * size.height * 0.5; final opacity = (1.0 - progress).clamp(0.0, 1.0); paint.color = particle.color.withOpacity(opacity); final rotation = progress * math.pi * 4; canvas.save(); canvas.translate(currentX, currentY); canvas.rotate(rotation); if (particle.size > 6) { _drawStar(canvas, paint, particle.size * scale); } else { canvas.drawCircle(Offset.zero, particle.size * scale, paint); } canvas.restore(); } if (progress < 0.3 && scale > 0) { _drawBurstEffect(canvas, size, progress, scale); } } void _drawStar(Canvas canvas, Paint paint, double size) { final path = Path(); final angleStep = (math.pi * 2) / 5; final innerRadius = size * 0.4; final outerRadius = size; for (int i = 0; i < 10; i++) { final angle = i * angleStep / 2; final radius = i % 2 == 0 ? outerRadius : innerRadius; final x = math.cos(angle) * radius; final y = math.sin(angle) * radius; if (i == 0) { path.moveTo(x, y); } else { path.lineTo(x, y); } } path.close(); canvas.drawPath(path, paint); } void _drawBurstEffect(Canvas canvas, Size size, double progress, double scale) { final center = Offset(size.width / 2, size.height / 2); final paint = Paint() ..style = PaintingStyle.stroke ..strokeWidth = 3.0; final colors = [ const Color(0xFFFFD700), const Color(0xFFFF6B35), const Color(0xFF4ECDC4), ]; for (int i = 0; i < colors.length; i++) { final radius = (progress * 150.0 * (i + 1)) * scale; final opacity = (1.0 - progress).clamp(0.0, 1.0); paint.color = colors[i].withOpacity(opacity * 0.5); canvas.drawCircle(center, radius, paint); } } @override bool shouldRepaint(covariant CustomPainter oldDelegate) => true; }