Blame view

lib/common/widgets/star_reward_widget.dart 3.62 KB
ae239ac7   吴启风   feat:星星动画封装(资源有点问题)
1
2
3
  import 'package:flutter/material.dart';
  import 'package:lottie/lottie.dart';
  
642081ad   吴启风   feat:lottie动画加载优化...
4
5
6
  import '../../utils/log_util.dart';
  
  class StarRewardWidget extends StatefulWidget {
ae239ac7   吴启风   feat:星星动画封装(资源有点问题)
7
8
9
10
11
12
    final double width;
    final double height;
    final bool isPlaying;
    final int starCount;
    final VoidCallback onAnimationEnd;
  
642081ad   吴启风   feat:lottie动画加载优化...
13
    const StarRewardWidget({
ae239ac7   吴启风   feat:星星动画封装(资源有点问题)
14
15
16
17
18
19
20
21
22
      Key? key,
      required this.width,
      required this.height,
      required this.isPlaying,
      required this.starCount,
      required this.onAnimationEnd,
    }) : super(key: key);
  
    @override
642081ad   吴启风   feat:lottie动画加载优化...
23
    _StarRewardWidgetState createState() => _StarRewardWidgetState();
ae239ac7   吴启风   feat:星星动画封装(资源有点问题)
24
25
  }
  
642081ad   吴启风   feat:lottie动画加载优化...
26
  class _StarRewardWidgetState extends State<StarRewardWidget>
ae239ac7   吴启风   feat:星星动画封装(资源有点问题)
27
28
      with SingleTickerProviderStateMixin {
    late final AnimationController _controller;
642081ad   吴启风   feat:lottie动画加载优化...
29
    late final Future<LottieComposition> _futureComposition;
ae239ac7   吴启风   feat:星星动画封装(资源有点问题)
30
    bool _isVisible = false;
642081ad   吴启风   feat:lottie动画加载优化...
31
    static const String TAG = "StarRewardWidget";
ae239ac7   吴启风   feat:星星动画封装(资源有点问题)
32
33
34
35
36
37
38
39
40
  
    @override
    void initState() {
      super.initState();
      _controller = AnimationController(vsync: this);
      _loadComposition();
    }
  
    @override
642081ad   吴启风   feat:lottie动画加载优化...
41
    void didUpdateWidget(StarRewardWidget oldWidget) {
ae239ac7   吴启风   feat:星星动画封装(资源有点问题)
42
43
44
45
46
47
      super.didUpdateWidget(oldWidget);
      if (widget.isPlaying && !_controller.isAnimating) {
        _startAnimation();
      }
    }
  
642081ad   吴启风   feat:lottie动画加载优化...
48
    void _loadComposition() {
ae239ac7   吴启风   feat:星星动画封装(资源有点问题)
49
50
51
52
53
54
      // final composition = await AssetLottie('assets/lotties/recorder_input.zip').load();
      // setState(() {
      //   _composition = composition;
      //   _controller.duration = _composition.duration;
      // });
  
642081ad   吴启风   feat:lottie动画加载优化...
55
      _futureComposition = _loadLottieComposition();
ae239ac7   吴启风   feat:星星动画封装(资源有点问题)
56
57
  
      if (widget.isPlaying) {
642081ad   吴启风   feat:lottie动画加载优化...
58
          _startAnimation();
ae239ac7   吴启风   feat:星星动画封装(资源有点问题)
59
60
61
      }
    }
  
642081ad   吴启风   feat:lottie动画加载优化...
62
63
    void _startAnimation() {
      Log.d("$TAG _startAnimation");
ae239ac7   吴启风   feat:星星动画封装(资源有点问题)
64
65
66
67
      setState(() {
        _isVisible = true;
      });
  
642081ad   吴启风   feat:lottie动画加载优化...
68
69
70
      _futureComposition.then((composition) {
        Log.d("$TAG _futureComposition.then duration=${composition.duration}");
        _controller.duration = composition.duration;
c623c7b2   吴启风   feat:语音跟读作答结果动效&语音
71
        _controller.forward().whenCompleteOrCancel(() {
642081ad   吴启风   feat:lottie动画加载优化...
72
73
74
75
          setState(() {
            _isVisible = false;
          });
          widget.onAnimationEnd(); // 调用外部回调函数
ae239ac7   吴启风   feat:星星动画封装(资源有点问题)
76
        });
ae239ac7   吴启风   feat:星星动画封装(资源有点问题)
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
      });
    }
  
    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;
      }
c623c7b2   吴启风   feat:语音跟读作答结果动效&语音
96
      return await AssetLottie(assetPath).load();
ae239ac7   吴启风   feat:星星动画封装(资源有点问题)
97
98
99
100
101
102
103
104
105
106
107
108
    }
  
    @override
    void dispose() {
      _controller.dispose();
      super.dispose();
    }
  
    @override
    Widget build(BuildContext context) {
      return Center(
        child: SizedBox(
ae239ac7   吴启风   feat:星星动画封装(资源有点问题)
109
110
            width: widget.width,
            height: widget.height,
642081ad   吴启风   feat:lottie动画加载优化...
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
            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,
            // ),
            ),
ae239ac7   吴启风   feat:星星动画封装(资源有点问题)
136
137
138
      );
    }
  }