star_reward_widget.dart
3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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<StarRewardWidget>
with SingleTickerProviderStateMixin {
late final AnimationController _controller;
late final Future<LottieComposition> _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 _startAnimation");
setState(() {
_isVisible = true;
});
_futureComposition.then((composition) {
Log.d("$TAG _futureComposition.then duration=${composition.duration}");
_controller.duration = composition.duration;
_controller.forward().whenCompleteOrCancel(() {
setState(() {
_isVisible = false;
});
widget.onAnimationEnd(); // 调用外部回调函数
});
});
}
Future<LottieComposition> _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<LottieComposition>(
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,
// ),
),
);
}
}