diff --git a/lib/common/utils/show_star_reward_dialog.dart b/lib/common/utils/show_star_reward_dialog.dart new file mode 100644 index 0000000..e36de90 --- /dev/null +++ b/lib/common/utils/show_star_reward_dialog.dart @@ -0,0 +1,28 @@ +import 'package:flutter/material.dart'; +import 'package:wow_english/common/widgets/star_reward_widget.dart'; + +void showStarRewardDialog(BuildContext context, { + double width = 200, + double height = 200, + int starCount = 3, +}) { + showDialog( + context: context, + barrierDismissible: false, // 点击对话框外部不关闭对话框 + builder: (BuildContext context) { + return Dialog( + backgroundColor: Colors.transparent, // 设置对话框背景为透明 + insetPadding: const EdgeInsets.all(0), // 去除对话框的内边距 + child: StarRewardAnimation( + width: width, + height: height, + isPlaying: true, + starCount: starCount, + onAnimationEnd: () { + Navigator.of(context).pop(); // 关闭对话框 + }, + ), + ); + }, + ); +} \ No newline at end of file diff --git a/lib/common/widgets/star_reward_widget.dart b/lib/common/widgets/star_reward_widget.dart new file mode 100644 index 0000000..215b2ed --- /dev/null +++ b/lib/common/widgets/star_reward_widget.dart @@ -0,0 +1,117 @@ +import 'package:flutter/material.dart'; +import 'package:lottie/lottie.dart'; + +class StarRewardAnimation extends StatefulWidget { + final double width; + final double height; + final bool isPlaying; + final int starCount; + final VoidCallback onAnimationEnd; + + const StarRewardAnimation({ + Key? key, + required this.width, + required this.height, + required this.isPlaying, + required this.starCount, + required this.onAnimationEnd, + }) : super(key: key); + + @override + _StarRewardAnimationState createState() => _StarRewardAnimationState(); +} + +class _StarRewardAnimationState extends State + with SingleTickerProviderStateMixin { + late final AnimationController _controller; + late final LottieComposition _composition; + bool _isVisible = false; + + @override + void initState() { + super.initState(); + _controller = AnimationController(vsync: this); + _loadComposition(); + } + + @override + void didUpdateWidget(StarRewardAnimation oldWidget) { + super.didUpdateWidget(oldWidget); + if (widget.isPlaying && !_controller.isAnimating) { + _startAnimation(); + } + } + + Future _loadComposition() async { + // final composition = await AssetLottie('assets/lotties/recorder_input.zip').load(); + // setState(() { + // _composition = composition; + // _controller.duration = _composition.duration; + // }); + + final composition = await _loadLottieComposition(); + setState(() { + _composition = composition; + _controller.duration = _composition.duration; + }); + + if (widget.isPlaying) { + _startAnimation(); + } + } + + void _startAnimation() async { + setState(() { + _isVisible = true; + }); + + _controller.forward().whenComplete(() { + 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: Lottie( + composition: _composition, + controller: _controller, + renderCache: RenderCache.raster, + width: widget.width, + height: widget.height, + ), + ), + ); + } +} diff --git a/pubspec.yaml b/pubspec.yaml index 4dc3f2b..b7fb8ae 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -154,11 +154,7 @@ flutter: - assets/fonts/ - assets/sounds/ - assets/lotties/ - - assets/lotties/recorder_input/images/ - - assets/lotties/recorder_playback/images/ - - assets/lotties/star1_fullscreen/ - - assets/lotties/star2_fullscreen/ - - assets/lotties/star3_fullscreen/ + fonts: - family: HannotateSC fonts: