import 'package:flutter/material.dart'; import 'package:lottie/lottie.dart'; import '../../utils/log_util.dart'; class StarRewardWidget extends StatefulWidget { final double width; final double height; final bool isPlaying; final int starCount; final VoidCallback onAnimationEnd; const StarRewardWidget({ Key? key, required this.width, required this.height, required this.isPlaying, required this.starCount, required this.onAnimationEnd, }) : super(key: key); @override _StarRewardWidgetState createState() => _StarRewardWidgetState(); } class _StarRewardWidgetState extends State with SingleTickerProviderStateMixin { late final AnimationController _controller; late final Future _futureComposition; bool _isVisible = false; static const String TAG = "StarRewardWidget"; @override void initState() { super.initState(); _controller = AnimationController(vsync: this); _loadComposition(); } @override void didUpdateWidget(StarRewardWidget oldWidget) { super.didUpdateWidget(oldWidget); if (widget.isPlaying && !_controller.isAnimating) { _startAnimation(); } } void _loadComposition() { // final composition = await AssetLottie('assets/lotties/recorder_input.zip').load(); // setState(() { // _composition = composition; // _controller.duration = _composition.duration; // }); _futureComposition = _loadLottieComposition(); if (widget.isPlaying) { _startAnimation(); } } void _startAnimation() { Log.d("$TAG ${identityHashCode(this)} _startAnimation"); setState(() { _isVisible = true; }); _futureComposition.then((composition) { Log.d("$TAG ${identityHashCode(this)} _futureComposition.then duration=${composition.duration}"); _controller.duration = composition.duration; _controller.forward().whenCompleteOrCancel(() { if (mounted) { setState(() { _isVisible = false; }); widget.onAnimationEnd(); // 调用外部回调函数 } }); }); } Future _loadLottieComposition() async { String assetPath; switch (widget.starCount) { case 1: assetPath = 'assets/lotties/star1_reward.zip'; break; case 2: assetPath = 'assets/lotties/star2_reward.zip'; break; case 3: assetPath = 'assets/lotties/star3_reward.zip'; break; default: assetPath = 'assets/lotties/star3_reward.zip'; break; } return await AssetLottie(assetPath).load(); } @override void dispose() { _controller.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Center( child: SizedBox( width: widget.width, height: widget.height, child: FutureBuilder( future: _futureComposition, builder: (context, snapshot) { if (snapshot.hasData) { final composition = snapshot.data!; return Lottie( composition: composition, controller: _controller, renderCache: RenderCache.raster, width: widget.width, height: widget.height, ); } else { return const SizedBox.shrink(); } }) // child: Lottie( // composition: _composition, // controller: _controller, // renderCache: RenderCache.raster, // width: widget.width, // height: widget.height, // ), ), ); } }