Blame view

lib/common/widgets/recorder_widget.dart 3.58 KB
d4d91cb0   吴启风   feat:lottie动效组件封装...
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
  import 'package:flutter/material.dart';
  import 'package:lottie/lottie.dart';
  import 'package:wow_english/common/widgets/throttledGesture_gesture_detector.dart';
  
  import '../../utils/log_util.dart';
  
  /// 录音组件
  class RecorderWidget extends StatefulWidget {
    final bool isClickable;
    final bool isPlaying;
    final VoidCallback? onTap;
    final double width;
    final double height;
  
    const RecorderWidget({
      Key? key,
      required this.isClickable,
      required this.isPlaying,
      required this.onTap,
      required this.width,
      required this.height,
    }) : super(key: key);
  
    @override
    _RecorderWidgetState createState() => _RecorderWidgetState();
  }
  
  class _RecorderWidgetState extends State<RecorderWidget>
      with SingleTickerProviderStateMixin {
    late final AnimationController _controller;
642081ad   吴启风   feat:lottie动画加载优化...
31
    late final Future<LottieComposition> _futureComposition;
d4d91cb0   吴启风   feat:lottie动效组件封装...
32
33
34
35
36
37
38
39
40
    static const String TAG = "RecorderWidget";
  
    bool _isPlaying = false;
  
    @override
    void initState() {
      super.initState();
      _controller = AnimationController(vsync: this);
      _loadComposition();
642081ad   吴启风   feat:lottie动画加载优化...
41
42
43
      if (widget.isPlaying) {
        _playInitialAnimation();
      }
d4d91cb0   吴启风   feat:lottie动效组件封装...
44
45
46
47
48
49
  
      _controller.addListener(() {
        // Log.d("$TAG addListener _controller=${_controller.status}");
      });
    }
  
642081ad   吴启风   feat:lottie动画加载优化...
50
51
52
53
54
55
56
57
58
    @override
    void didUpdateWidget(RecorderWidget oldWidget) {
      super.didUpdateWidget(oldWidget);
      Log.d(
          "$TAG didUpdateWidget widget=${widget.isPlaying} oldWidget=${oldWidget.isPlaying} _isPlaying=$_isPlaying");
      if (widget.isPlaying && !_isPlaying) {
        setState(() {
          _isPlaying = true;
        });
d4d91cb0   吴启风   feat:lottie动效组件封装...
59
        _playInitialAnimation();
642081ad   吴启风   feat:lottie动画加载优化...
60
61
      } else if (!widget.isPlaying && _isPlaying) {
        _playFinalAnimation();
d4d91cb0   吴启风   feat:lottie动效组件封装...
62
63
64
      }
    }
  
642081ad   吴启风   feat:lottie动画加载优化...
65
66
67
68
69
    Future<void> _loadComposition() async {
      _futureComposition =
          AssetLottie('assets/lotties/recorder_input.zip').load();
    }
  
d4d91cb0   吴启风   feat:lottie动效组件封装...
70
71
    void _playInitialAnimation() {
      _controller.reset();
642081ad   吴启风   feat:lottie动画加载优化...
72
73
74
75
76
77
      _futureComposition.then((composition) {
        _controller.duration = composition.duration;
        _controller
            .animateTo(22 / composition.endFrame)
            .whenComplete(() => _loopMiddleAnimation(composition));
      });
d4d91cb0   吴启风   feat:lottie动效组件封装...
78
79
    }
  
642081ad   吴启风   feat:lottie动画加载优化...
80
    void _loopMiddleAnimation(LottieComposition composition) {
d4d91cb0   吴启风   feat:lottie动效组件封装...
81
      _controller.repeat(
642081ad   吴启风   feat:lottie动画加载优化...
82
83
        min: 22 / composition.endFrame,
        max: 37 / composition.endFrame,
d4d91cb0   吴启风   feat:lottie动效组件封装...
84
85
86
87
88
      );
    }
  
    void _playFinalAnimation() {
      _controller.stop();
642081ad   吴启风   feat:lottie动画加载优化...
89
90
91
92
93
94
      _futureComposition.then((composition) {
        _controller.duration = composition.duration;
        _controller
            .animateTo(50 / composition.endFrame)
            .whenComplete(() => _resetAnimation());
      });
d4d91cb0   吴启风   feat:lottie动效组件封装...
95
96
97
98
99
100
101
102
103
104
    }
  
    void _resetAnimation() {
      setState(() {
        _isPlaying = false;
        _controller.value = 0;
      });
    }
  
    @override
d4d91cb0   吴启风   feat:lottie动效组件封装...
105
106
107
108
109
110
111
112
113
114
    void dispose() {
      _controller.dispose();
      super.dispose();
    }
  
    @override
    Widget build(BuildContext context) {
      return ThrottledGestureDetector(
        onTap: widget.isClickable ? widget.onTap : null,
        child: Opacity(
642081ad   吴启风   feat:lottie动画加载优化...
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
            opacity: widget.isClickable ? 1.0 : 0.5, // 设置透明度
            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();
                  }
                })),
d4d91cb0   吴启风   feat:lottie动效组件封装...
132
133
134
      );
    }
  }