rotate_image.dart 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import 'package:flutter/cupertino.dart';
  2. /// 旋转图片组件
  3. class RotateImage extends StatefulWidget {
  4. /// 图片组件
  5. final Image image;
  6. /// 动画时长
  7. final Duration duration;
  8. const RotateImage({
  9. super.key,
  10. required this.image,
  11. this.duration = const Duration(milliseconds: 1000),
  12. });
  13. @override
  14. State<StatefulWidget> createState() {
  15. return _RotateImageState();
  16. }
  17. }
  18. class _RotateImageState extends State<RotateImage>
  19. with SingleTickerProviderStateMixin {
  20. /// 动画控制器
  21. late AnimationController _animationController;
  22. @override
  23. void initState() {
  24. super.initState();
  25. _animationController = AnimationController(duration: widget.duration, vsync: this)
  26. // 无限循环
  27. ..repeat();
  28. }
  29. @override
  30. void dispose() {
  31. // 释放资源
  32. _animationController.dispose();
  33. super.dispose();
  34. }
  35. @override
  36. Widget build(BuildContext context) {
  37. // 动画组件
  38. return RotationTransition(
  39. turns: _animationController,
  40. child: widget.image,
  41. );
  42. }
  43. }