Commit f946f1ce88be2be0383ccc2509ba8e9c253e160b
1 parent
79de0824
feat:小鹅通入口图片定时动画
Showing
1 changed file
with
21 additions
and
3 deletions
lib/pages/home/widgets/ShakeImage.dart
1 | 1 | ||
2 | +import 'dart:async'; | ||
3 | + | ||
2 | import 'package:flutter/cupertino.dart'; | 4 | import 'package:flutter/cupertino.dart'; |
3 | import 'package:wow_english/common/extension/string_extension.dart'; | 5 | import 'package:wow_english/common/extension/string_extension.dart'; |
4 | 6 | ||
@@ -10,13 +12,17 @@ class ShakeImage extends StatefulWidget { | @@ -10,13 +12,17 @@ class ShakeImage extends StatefulWidget { | ||
10 | _ShakeImageState createState() => _ShakeImageState(); | 12 | _ShakeImageState createState() => _ShakeImageState(); |
11 | } | 13 | } |
12 | 14 | ||
13 | -class _ShakeImageState extends State<ShakeImage> with SingleTickerProviderStateMixin { | 15 | +class _ShakeImageState extends State<ShakeImage> with SingleTickerProviderStateMixin, |
16 | + WidgetsBindingObserver { | ||
14 | late AnimationController _controller; | 17 | late AnimationController _controller; |
15 | late Animation<double> _animation; | 18 | late Animation<double> _animation; |
19 | + late Timer _timer; | ||
16 | 20 | ||
17 | @override | 21 | @override |
18 | void initState() { | 22 | void initState() { |
19 | super.initState(); | 23 | super.initState(); |
24 | + WidgetsBinding.instance.addObserver(this); | ||
25 | + | ||
20 | _controller = AnimationController( | 26 | _controller = AnimationController( |
21 | duration: const Duration(seconds: 2), | 27 | duration: const Duration(seconds: 2), |
22 | vsync: this, | 28 | vsync: this, |
@@ -32,8 +38,9 @@ class _ShakeImageState extends State<ShakeImage> with SingleTickerProviderStateM | @@ -32,8 +38,9 @@ class _ShakeImageState extends State<ShakeImage> with SingleTickerProviderStateM | ||
32 | TweenSequenceItem(tween: Tween(begin: -0.05, end: 0.0).chain(CurveTween(curve: Curves.easeInOut)), weight: 1), | 38 | TweenSequenceItem(tween: Tween(begin: -0.05, end: 0.0).chain(CurveTween(curve: Curves.easeInOut)), weight: 1), |
33 | ]).animate(_controller); | 39 | ]).animate(_controller); |
34 | 40 | ||
35 | - Future.delayed(const Duration(seconds: 1), () { | ||
36 | - _controller.forward(); | 41 | + _controller.forward(from: 0.0); |
42 | + _timer = Timer.periodic(const Duration(seconds: 4), (Timer timer) { | ||
43 | + _controller.forward(from: 0.0); | ||
37 | }); | 44 | }); |
38 | 45 | ||
39 | // _controller.addStatusListener((status) { | 46 | // _controller.addStatusListener((status) { |
@@ -46,8 +53,19 @@ class _ShakeImageState extends State<ShakeImage> with SingleTickerProviderStateM | @@ -46,8 +53,19 @@ class _ShakeImageState extends State<ShakeImage> with SingleTickerProviderStateM | ||
46 | } | 53 | } |
47 | 54 | ||
48 | @override | 55 | @override |
56 | + void didChangeAppLifecycleState(AppLifecycleState state) { | ||
57 | + if (state == AppLifecycleState.paused) { | ||
58 | + _controller.stop(); | ||
59 | + } else if (state == AppLifecycleState.resumed) { | ||
60 | + _controller.forward(); | ||
61 | + } | ||
62 | + } | ||
63 | + | ||
64 | + @override | ||
49 | void dispose() { | 65 | void dispose() { |
66 | + WidgetsBinding.instance.removeObserver(this); | ||
50 | _controller.dispose(); | 67 | _controller.dispose(); |
68 | + _timer.cancel(); | ||
51 | super.dispose(); | 69 | super.dispose(); |
52 | } | 70 | } |
53 | 71 |