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;
|
c272c662
吴启风
fix: 崩溃修复
|
23
|
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();
|
c272c662
吴启风
fix: 崩溃修复
|
46
47
48
49
50
|
try {
WidgetsBinding.instance.addObserver(this);
} catch (e) {
printLog('Add observer error: $e');
}
|
8a556f76
吴启风
feat:性能优化-首页'wow精...
|
51
52
53
54
55
56
|
WidgetsBinding.instance.addPostFrameCallback((_) {
final route = ModalRoute.of(context);
if (route is PageRoute) {
customerRouteObserver.subscribe(this, route);
}
});
|
f946f1ce
吴启风
feat:小鹅通入口图片定时动画
|
57
|
|
79de0824
吴启风
feat:小鹅通入口图片增加摇摆动画
|
58
59
60
61
62
63
|
_controller = AnimationController(
duration: const Duration(seconds: 2),
vsync: this,
);
_animation = TweenSequence([
|
8a556f76
吴启风
feat:性能优化-首页'wow精...
|
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
|
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:小鹅通入口图片增加摇摆动画
|
92
93
|
]).animate(_controller);
|
8a556f76
吴启风
feat:性能优化-首页'wow精...
|
94
|
printLog("--initState");
|
79de0824
吴启风
feat:小鹅通入口图片增加摇摆动画
|
95
96
97
98
99
100
101
102
103
104
|
// _controller.addStatusListener((status) {
// if (status == AnimationStatus.completed) {
// _controller.reverse();
// } else if (status == AnimationStatus.dismissed) {
// _controller.forward();
// }
// });
}
|
8a556f76
吴启风
feat:性能优化-首页'wow精...
|
105
106
|
void _startAnimation() {
Timer(const Duration(seconds: 1), () {
|
c272c662
吴启风
fix: 崩溃修复
|
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
// 检查widget是否还存在且controller是否有效
if (mounted) {
try {
_controller.forward(from: 0.0);
_timer = Timer.periodic(const Duration(seconds: 4), (Timer timer) {
// 每次执行前都检查widget状态和controller状态
if (mounted) {
try {
_controller.forward(from: 0.0);
} catch (e) {
printLog('Timer callback error: $e');
timer.cancel();
}
}
});
} catch (e) {
printLog('Initial animation error: $e');
}
}
|
8a556f76
吴启风
feat:性能优化-首页'wow精...
|
126
127
128
129
130
|
});
printLog('_startAnimation');
}
void _stopAnimation() {
|
c272c662
吴启风
fix: 崩溃修复
|
131
132
|
_timer?.cancel();
_timer = null;
|
8a556f76
吴启风
feat:性能优化-首页'wow精...
|
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
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:小鹅通入口图片增加摇摆动画
|
182
|
@override
|
f946f1ce
吴启风
feat:小鹅通入口图片定时动画
|
183
184
|
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.paused) {
|
c272c662
吴启风
fix: 崩溃修复
|
185
186
187
188
189
190
191
|
if (mounted) {
try {
_controller.stop();
} catch (e) {
printLog('Stop animation error: $e');
}
}
|
f946f1ce
吴启风
feat:小鹅通入口图片定时动画
|
192
|
} else if (state == AppLifecycleState.resumed) {
|
c272c662
吴启风
fix: 崩溃修复
|
193
194
195
196
197
198
199
|
if (mounted) {
try {
_controller.forward();
} catch (e) {
printLog('Resume animation error: $e');
}
}
|
f946f1ce
吴启风
feat:小鹅通入口图片定时动画
|
200
201
202
203
|
}
}
@override
|
79de0824
吴启风
feat:小鹅通入口图片增加摇摆动画
|
204
|
void dispose() {
|
8a556f76
吴启风
feat:性能优化-首页'wow精...
|
205
206
|
printLog('dispose');
customerRouteObserver.unsubscribe(this);
|
c272c662
吴启风
fix: 崩溃修复
|
207
208
209
210
211
|
try {
WidgetsBinding.instance.removeObserver(this);
} catch (e) {
printLog('Remove observer error: $e');
}
|
79de0824
吴启风
feat:小鹅通入口图片增加摇摆动画
|
212
|
_controller.dispose();
|
c272c662
吴启风
fix: 崩溃修复
|
213
|
_timer?.cancel();
|
79de0824
吴启风
feat:小鹅通入口图片增加摇摆动画
|
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
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精...
|
228
|
child: Image.asset('xe_shop'.assetPng, width: 153),
|
79de0824
吴启风
feat:小鹅通入口图片增加摇摆动画
|
229
230
231
|
),
);
}
|
8a556f76
吴启风
feat:性能优化-首页'wow精...
|
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
/// 监听状态
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');
}
}
|