Blame view

lib/common/widgets/cheer_reward_widget.dart 2.87 KB
c623c7b2   吴启风   feat:语音跟读作答结果动效&语音
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
  import 'package:flutter/material.dart';
  import 'package:lottie/lottie.dart';
  
  import '../../utils/log_util.dart';
  
  class CheerRewardWidget extends StatefulWidget {
    final String lottieFile;
    final double width;
    final double height;
    final bool isPlaying;
    final VoidCallback onAnimationEnd;
  
    const CheerRewardWidget({
      Key? key,
      required this.lottieFile,
      required this.width,
      required this.height,
      required this.isPlaying,
      required this.onAnimationEnd,
    }) : super(key: key);
  
    @override
    _CheerRewardWidgetState createState() => _CheerRewardWidgetState();
  }
  
  class _CheerRewardWidgetState extends State<CheerRewardWidget>
      with SingleTickerProviderStateMixin {
    late final AnimationController _controller;
    late final Future<LottieComposition> _futureComposition;
    bool _isVisible = false;
    static const String TAG = "CheerRewardWidget";
  
    @override
    void initState() {
      super.initState();
      _controller = AnimationController(vsync: this);
      _loadComposition();
    }
  
    @override
    void didUpdateWidget(CheerRewardWidget oldWidget) {
      super.didUpdateWidget(oldWidget);
      if (widget.isPlaying && !_controller.isAnimating) {
        _startAnimation();
      }
    }
  
    void _loadComposition() {
c623c7b2   吴启风   feat:语音跟读作答结果动效&语音
49
50
51
      _futureComposition = _loadLottieComposition();
  
      if (widget.isPlaying) {
0ebc6186   吴启风   feat:解决异步场景下setSt...
52
        _startAnimation();
c623c7b2   吴启风   feat:语音跟读作答结果动效&语音
53
54
55
56
      }
    }
  
    void _startAnimation() {
819ae43b   吴启风   feat:体验优化-练习题目取消阻...
57
      Log.d("$TAG ${identityHashCode(this)} _startAnimation");
c623c7b2   吴启风   feat:语音跟读作答结果动效&语音
58
59
60
61
62
      setState(() {
        _isVisible = true;
      });
  
      _futureComposition.then((composition) {
819ae43b   吴启风   feat:体验优化-练习题目取消阻...
63
        Log.d("$TAG ${identityHashCode(this)} _futureComposition.then duration=${composition.duration}");
c623c7b2   吴启风   feat:语音跟读作答结果动效&语音
64
65
        _controller.duration = composition.duration;
        _controller.forward().whenCompleteOrCancel(() {
0ebc6186   吴启风   feat:解决异步场景下setSt...
66
67
68
69
70
71
          if (mounted) {
            setState(() {
              _isVisible = false;
            });
            widget.onAnimationEnd(); // 调用外部回调函数
          }
c623c7b2   吴启风   feat:语音跟读作答结果动效&语音
72
73
74
75
76
        });
      });
    }
  
    Future<LottieComposition> _loadLottieComposition() async {
13de08d7   吴启风   feat:星星动效使用纯json代...
77
      return AssetLottie(widget.lottieFile).load();
c623c7b2   吴启风   feat:语音跟读作答结果动效&语音
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
    }
  
    @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();
                  }
0ebc6186   吴启风   feat:解决异步场景下setSt...
107
                })),
c623c7b2   吴启风   feat:语音跟读作答结果动效&语音
108
109
110
      );
    }
  }