Commit ae239ac7e34c85f91741413d534148e727622dd1
1 parent
d4d91cb0
feat:星星动画封装(资源有点问题)
Showing
3 changed files
with
146 additions
and
5 deletions
lib/common/utils/show_star_reward_dialog.dart
0 → 100644
| 1 | +import 'package:flutter/material.dart'; | ||
| 2 | +import 'package:wow_english/common/widgets/star_reward_widget.dart'; | ||
| 3 | + | ||
| 4 | +void showStarRewardDialog(BuildContext context, { | ||
| 5 | + double width = 200, | ||
| 6 | + double height = 200, | ||
| 7 | + int starCount = 3, | ||
| 8 | +}) { | ||
| 9 | + showDialog( | ||
| 10 | + context: context, | ||
| 11 | + barrierDismissible: false, // 点击对话框外部不关闭对话框 | ||
| 12 | + builder: (BuildContext context) { | ||
| 13 | + return Dialog( | ||
| 14 | + backgroundColor: Colors.transparent, // 设置对话框背景为透明 | ||
| 15 | + insetPadding: const EdgeInsets.all(0), // 去除对话框的内边距 | ||
| 16 | + child: StarRewardAnimation( | ||
| 17 | + width: width, | ||
| 18 | + height: height, | ||
| 19 | + isPlaying: true, | ||
| 20 | + starCount: starCount, | ||
| 21 | + onAnimationEnd: () { | ||
| 22 | + Navigator.of(context).pop(); // 关闭对话框 | ||
| 23 | + }, | ||
| 24 | + ), | ||
| 25 | + ); | ||
| 26 | + }, | ||
| 27 | + ); | ||
| 28 | +} | ||
| 0 | \ No newline at end of file | 29 | \ No newline at end of file |
lib/common/widgets/star_reward_widget.dart
0 → 100644
| 1 | +import 'package:flutter/material.dart'; | ||
| 2 | +import 'package:lottie/lottie.dart'; | ||
| 3 | + | ||
| 4 | +class StarRewardAnimation extends StatefulWidget { | ||
| 5 | + final double width; | ||
| 6 | + final double height; | ||
| 7 | + final bool isPlaying; | ||
| 8 | + final int starCount; | ||
| 9 | + final VoidCallback onAnimationEnd; | ||
| 10 | + | ||
| 11 | + const StarRewardAnimation({ | ||
| 12 | + Key? key, | ||
| 13 | + required this.width, | ||
| 14 | + required this.height, | ||
| 15 | + required this.isPlaying, | ||
| 16 | + required this.starCount, | ||
| 17 | + required this.onAnimationEnd, | ||
| 18 | + }) : super(key: key); | ||
| 19 | + | ||
| 20 | + @override | ||
| 21 | + _StarRewardAnimationState createState() => _StarRewardAnimationState(); | ||
| 22 | +} | ||
| 23 | + | ||
| 24 | +class _StarRewardAnimationState extends State<StarRewardAnimation> | ||
| 25 | + with SingleTickerProviderStateMixin { | ||
| 26 | + late final AnimationController _controller; | ||
| 27 | + late final LottieComposition _composition; | ||
| 28 | + bool _isVisible = false; | ||
| 29 | + | ||
| 30 | + @override | ||
| 31 | + void initState() { | ||
| 32 | + super.initState(); | ||
| 33 | + _controller = AnimationController(vsync: this); | ||
| 34 | + _loadComposition(); | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + @override | ||
| 38 | + void didUpdateWidget(StarRewardAnimation oldWidget) { | ||
| 39 | + super.didUpdateWidget(oldWidget); | ||
| 40 | + if (widget.isPlaying && !_controller.isAnimating) { | ||
| 41 | + _startAnimation(); | ||
| 42 | + } | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + Future<void> _loadComposition() async { | ||
| 46 | + // final composition = await AssetLottie('assets/lotties/recorder_input.zip').load(); | ||
| 47 | + // setState(() { | ||
| 48 | + // _composition = composition; | ||
| 49 | + // _controller.duration = _composition.duration; | ||
| 50 | + // }); | ||
| 51 | + | ||
| 52 | + final composition = await _loadLottieComposition(); | ||
| 53 | + setState(() { | ||
| 54 | + _composition = composition; | ||
| 55 | + _controller.duration = _composition.duration; | ||
| 56 | + }); | ||
| 57 | + | ||
| 58 | + if (widget.isPlaying) { | ||
| 59 | + _startAnimation(); | ||
| 60 | + } | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + void _startAnimation() async { | ||
| 64 | + setState(() { | ||
| 65 | + _isVisible = true; | ||
| 66 | + }); | ||
| 67 | + | ||
| 68 | + _controller.forward().whenComplete(() { | ||
| 69 | + setState(() { | ||
| 70 | + _isVisible = false; | ||
| 71 | + }); | ||
| 72 | + widget.onAnimationEnd(); // 调用外部回调函数 | ||
| 73 | + }); | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + Future<LottieComposition> _loadLottieComposition() async { | ||
| 77 | + String assetPath; | ||
| 78 | + switch (widget.starCount) { | ||
| 79 | + case 1: | ||
| 80 | + assetPath = 'assets/lotties/star1_reward.zip'; | ||
| 81 | + break; | ||
| 82 | + case 2: | ||
| 83 | + assetPath = 'assets/lotties/star2_reward.zip'; | ||
| 84 | + break; | ||
| 85 | + case 3: | ||
| 86 | + assetPath = 'assets/lotties/star3_reward.zip'; | ||
| 87 | + break; | ||
| 88 | + default: | ||
| 89 | + assetPath = 'assets/lotties/star3_reward.zip'; | ||
| 90 | + break; | ||
| 91 | + } | ||
| 92 | + return await AssetLottie(assetPath).load(); | ||
| 93 | + } | ||
| 94 | + | ||
| 95 | + @override | ||
| 96 | + void dispose() { | ||
| 97 | + _controller.dispose(); | ||
| 98 | + super.dispose(); | ||
| 99 | + } | ||
| 100 | + | ||
| 101 | + @override | ||
| 102 | + Widget build(BuildContext context) { | ||
| 103 | + return Center( | ||
| 104 | + child: SizedBox( | ||
| 105 | + width: widget.width, | ||
| 106 | + height: widget.height, | ||
| 107 | + child: Lottie( | ||
| 108 | + composition: _composition, | ||
| 109 | + controller: _controller, | ||
| 110 | + renderCache: RenderCache.raster, | ||
| 111 | + width: widget.width, | ||
| 112 | + height: widget.height, | ||
| 113 | + ), | ||
| 114 | + ), | ||
| 115 | + ); | ||
| 116 | + } | ||
| 117 | +} |
pubspec.yaml
| @@ -154,11 +154,7 @@ flutter: | @@ -154,11 +154,7 @@ flutter: | ||
| 154 | - assets/fonts/ | 154 | - assets/fonts/ |
| 155 | - assets/sounds/ | 155 | - assets/sounds/ |
| 156 | - assets/lotties/ | 156 | - assets/lotties/ |
| 157 | - - assets/lotties/recorder_input/images/ | ||
| 158 | - - assets/lotties/recorder_playback/images/ | ||
| 159 | - - assets/lotties/star1_fullscreen/ | ||
| 160 | - - assets/lotties/star2_fullscreen/ | ||
| 161 | - - assets/lotties/star3_fullscreen/ | 157 | + |
| 162 | fonts: | 158 | fonts: |
| 163 | - family: HannotateSC | 159 | - family: HannotateSC |
| 164 | fonts: | 160 | fonts: |