import 'dart:async'; import 'package:flutter/cupertino.dart'; import 'package:wow_english/common/extension/string_extension.dart'; ///带左右摇晃的wow封面 class ShakeImage extends StatefulWidget { const ShakeImage({super.key}); @override _ShakeImageState createState() => _ShakeImageState(); } class _ShakeImageState extends State with SingleTickerProviderStateMixin, WidgetsBindingObserver { late AnimationController _controller; late Animation _animation; late Timer _timer; @override void initState() { super.initState(); WidgetsBinding.instance.addObserver(this); _controller = AnimationController( duration: const Duration(seconds: 2), vsync: this, ); _animation = TweenSequence([ 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), ]).animate(_controller); Timer(const Duration(seconds: 1), () { _controller.forward(from: 0.0); _timer = Timer.periodic(const Duration(seconds: 4), (Timer timer) { _controller.forward(from: 0.0); }); }); // _controller.addStatusListener((status) { // if (status == AnimationStatus.completed) { // _controller.reverse(); // } else if (status == AnimationStatus.dismissed) { // _controller.forward(); // } // }); } @override void didChangeAppLifecycleState(AppLifecycleState state) { if (state == AppLifecycleState.paused) { _controller.stop(); } else if (state == AppLifecycleState.resumed) { _controller.forward(); } } @override void dispose() { WidgetsBinding.instance.removeObserver(this); _controller.dispose(); _timer.cancel(); super.dispose(); } @override Widget build(BuildContext context) { return Center( child: AnimatedBuilder( animation: _animation, builder: (context, child) { return Transform.rotate( angle: _animation.value, child: child, ); }, child: Image.asset('xe_shop'.assetPng, width: 153), ), ); } }