Blame view

lib/pages/home/widgets/ShakeImage.dart 6.81 KB
f946f1ce   吴启风   feat:小鹅通入口图片定时动画
1
  import 'dart:async';
8a556f76   吴启风   feat:性能优化-首页'wow精...
2
  import 'dart:ui';
f946f1ce   吴启风   feat:小鹅通入口图片定时动画
3
  
79de0824   吴启风   feat:小鹅通入口图片增加摇摆动画
4
  import 'package:flutter/cupertino.dart';
8a556f76   吴启风   feat:性能优化-首页'wow精...
5
  import 'package:flutter/scheduler.dart';
79de0824   吴启风   feat:小鹅通入口图片增加摇摆动画
6
7
  import 'package:wow_english/common/extension/string_extension.dart';
  
8a556f76   吴启风   feat:性能优化-首页'wow精...
8
9
10
  import '../../../app/app.dart';
  import '../../../utils/log_util.dart';
  
79de0824   吴启风   feat:小鹅通入口图片增加摇摆动画
11
12
13
14
15
16
17
18
  ///带左右摇晃的wow封面
  class ShakeImage extends StatefulWidget {
    const ShakeImage({super.key});
  
    @override
    _ShakeImageState createState() => _ShakeImageState();
  }
  
8a556f76   吴启风   feat:性能优化-首页'wow精...
19
20
  class _ShakeImageState extends State<ShakeImage>
      with SingleTickerProviderStateMixin, WidgetsBindingObserver, RouteAware {
79de0824   吴启风   feat:小鹅通入口图片增加摇摆动画
21
22
    late AnimationController _controller;
    late Animation<double> _animation;
f946f1ce   吴启风   feat:小鹅通入口图片定时动画
23
    late Timer _timer;
8a556f76   吴启风   feat:性能优化-首页'wow精...
24
25
    late final AppLifecycleListener appLifecycleListener;
    static const TAG = 'ShakeImage';
79de0824   吴启风   feat:小鹅通入口图片增加摇摆动画
26
27
28
  
    @override
    void initState() {
8a556f76   吴启风   feat:性能优化-首页'wow精...
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
      var lifecycleState = SchedulerBinding.instance.lifecycleState;
      printLog(
          'lifecycleState:$lifecycleState'); // lifecycleState:AppLifecycleState.resumed
  
      appLifecycleListener = AppLifecycleListener(
        onStateChange: onStateChange,
        onResume: onResume,
        onInactive: onInactive,
        onHide: onHide,
        onShow: onShow,
        onPause: onPause,
        onRestart: onRestart,
        onDetach: onDetach,
        onExitRequested: onExitRequested,
      );
  
79de0824   吴启风   feat:小鹅通入口图片增加摇摆动画
45
      super.initState();
f946f1ce   吴启风   feat:小鹅通入口图片定时动画
46
      WidgetsBinding.instance.addObserver(this);
8a556f76   吴启风   feat:性能优化-首页'wow精...
47
48
49
50
51
52
      WidgetsBinding.instance.addPostFrameCallback((_) {
        final route = ModalRoute.of(context);
        if (route is PageRoute) {
          customerRouteObserver.subscribe(this, route);
        }
      });
f946f1ce   吴启风   feat:小鹅通入口图片定时动画
53
  
79de0824   吴启风   feat:小鹅通入口图片增加摇摆动画
54
55
56
57
58
59
      _controller = AnimationController(
        duration: const Duration(seconds: 2),
        vsync: this,
      );
  
      _animation = TweenSequence([
8a556f76   吴启风   feat:性能优化-首页'wow精...
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
        TweenSequenceItem(
            tween: Tween(begin: 0.0, end: 0.05)
                .chain(CurveTween(curve: Curves.easeInOut)),
            weight: 1),
        TweenSequenceItem(
            tween: Tween(begin: 0.05, end: -0.1)
                .chain(CurveTween(curve: Curves.easeInOut)),
            weight: 1),
        TweenSequenceItem(
            tween: Tween(begin: -0.1, end: 0.2)
                .chain(CurveTween(curve: Curves.easeInOut)),
            weight: 1),
        TweenSequenceItem(
            tween: Tween(begin: 0.2, end: -0.2)
                .chain(CurveTween(curve: Curves.easeInOut)),
            weight: 1),
        TweenSequenceItem(
            tween: Tween(begin: -0.2, end: 0.1)
                .chain(CurveTween(curve: Curves.easeInOut)),
            weight: 1),
        TweenSequenceItem(
            tween: Tween(begin: 0.1, end: -0.05)
                .chain(CurveTween(curve: Curves.easeInOut)),
            weight: 1),
        TweenSequenceItem(
            tween: Tween(begin: -0.05, end: 0.0)
                .chain(CurveTween(curve: Curves.easeInOut)),
            weight: 1),
79de0824   吴启风   feat:小鹅通入口图片增加摇摆动画
88
89
      ]).animate(_controller);
  
8a556f76   吴启风   feat:性能优化-首页'wow精...
90
      printLog("--initState");
79de0824   吴启风   feat:小鹅通入口图片增加摇摆动画
91
92
93
94
95
96
97
98
99
100
  
      // _controller.addStatusListener((status) {
      //   if (status == AnimationStatus.completed) {
      //     _controller.reverse();
      //   } else if (status == AnimationStatus.dismissed) {
      //     _controller.forward();
      //   }
      // });
    }
  
8a556f76   吴启风   feat:性能优化-首页'wow精...
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
    void _startAnimation() {
      Timer(const Duration(seconds: 1), () {
        _controller.forward(from: 0.0);
        _timer = Timer.periodic(const Duration(seconds: 4), (Timer timer) {
          _controller.forward(from: 0.0);
        });
      });
      printLog('_startAnimation');
    }
  
    void _stopAnimation() {
      _timer.cancel();
      printLog('_stopAnimation');
    }
  
    ///进下一页
    @override
    void didPushNext() {
      super.didPushNext();
      printLog('--didPushNext');
      _stopAnimation();
    }
  
    ///下一个页面退回到当前页
    @override
    void didPopNext() {
      printLog('--didPopNext');
      super.didPopNext();
      _startAnimation();
    }
  
    ///进入到当前页,在initState之后执行
    @override
    void didPush() {
      super.didPush();
      printLog('--didPush');
      _startAnimation();
    }
  
    ///退出当前页到上一页,监听导航栈中的路由被弹出(即当前页面被移除)的事件
    @override
    void didPop() {
      super.didPop();
      printLog('--didPop');
      _stopAnimation();
    }
  
    @override
    Future<bool> didPushRouteInformation(RouteInformation routeInformation) {
      printLog('--didPushRouteInformation routeInformation=$routeInformation');
      return super.didPushRouteInformation(routeInformation);
    }
  
    ///系统导航栏上的后退按钮(Android)或物理返回按钮被按下时调用,这个方法允许你拦截和处理系统的返回操作。
    @override
    Future<bool> didPopRoute() {
      printLog('--didPopRoute');
      return super.didPopRoute();
    }
  
    ///前后台切换
79de0824   吴启风   feat:小鹅通入口图片增加摇摆动画
162
    @override
f946f1ce   吴启风   feat:小鹅通入口图片定时动画
163
164
165
166
167
168
169
170
171
    void didChangeAppLifecycleState(AppLifecycleState state) {
      if (state == AppLifecycleState.paused) {
        _controller.stop();
      } else if (state == AppLifecycleState.resumed) {
        _controller.forward();
      }
    }
  
    @override
79de0824   吴启风   feat:小鹅通入口图片增加摇摆动画
172
    void dispose() {
8a556f76   吴启风   feat:性能优化-首页'wow精...
173
174
      printLog('dispose');
      customerRouteObserver.unsubscribe(this);
f946f1ce   吴启风   feat:小鹅通入口图片定时动画
175
      WidgetsBinding.instance.removeObserver(this);
79de0824   吴启风   feat:小鹅通入口图片增加摇摆动画
176
      _controller.dispose();
f946f1ce   吴启风   feat:小鹅通入口图片定时动画
177
      _timer.cancel();
79de0824   吴启风   feat:小鹅通入口图片增加摇摆动画
178
179
180
181
182
183
184
185
186
187
188
189
190
191
      super.dispose();
    }
  
    @override
    Widget build(BuildContext context) {
      return Center(
        child: AnimatedBuilder(
          animation: _animation,
          builder: (context, child) {
            return Transform.rotate(
              angle: _animation.value,
              child: child,
            );
          },
8a556f76   吴启风   feat:性能优化-首页'wow精...
192
          child: Image.asset('xe_shop'.assetPng, width: 153),
79de0824   吴启风   feat:小鹅通入口图片增加摇摆动画
193
194
195
        ),
      );
    }
8a556f76   吴启风   feat:性能优化-首页'wow精...
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
  
    /// 监听状态
    onStateChange(AppLifecycleState state) {
      printLog('app_state:$state');
    }
  
    // =============================== 根据App状态的产生的各种回调 ===============================
  
    /// 可见,并且可以响应用户操作时的回调
    /// 比如应用从后台调度到前台时,在 onShow() 后面 执行
    /// 注意:这个回调,初始化时 不执行
    onResume() {
      printLog('---onResume');
      _startAnimation();
    }
  
    /// 可见,但无法响应用户操作时的回调
    onInactive() {
      printLog('---onInactive');
    }
  
    /// 隐藏时的回调
    onHide() {
      printLog('---onHide');
    }
  
    /// 显示时的回调,应用从后台调度到前台时
    onShow() {
      printLog('---onShow');
    }
  
    /// 暂停时的回调(后台)
    onPause() {
      printLog('---onPause');
      _stopAnimation();
    }
  
    /// 暂停后恢复时的回调
    onRestart() {
      printLog('---onRestart');
    }
  
    /// 这两个回调,不是所有平台都支持,
    /// 当退出 并将所有视图与引擎分离时的回调(IOS 支持,Android 不支持)
    onDetach() {
      printLog('---onDetach');
    }
  
    /// 在退出程序时,发出询问的回调(IOS、Android 都不支持)
    /// 响应 [AppExitResponse.exit] 将继续终止,响应 [AppExitResponse.cancel] 将取消终止。
    Future<AppExitResponse> onExitRequested() async {
      printLog('---onExitRequested');
      return AppExitResponse.exit;
    }
  
    void printLog(String msg) {
      // Log.d('$TAG $msg');
    }
  }