animation_for_rotation.dart 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import 'package:flutter/material.dart';
  2. // 旋转动画
  3. class AnimationForRotation extends StatefulWidget {
  4. final Duration duration;
  5. final bool play;
  6. final Widget child;
  7. const AnimationForRotation({
  8. Key? key,
  9. required this.duration,
  10. required this.child,
  11. this.play = true,
  12. }) : super(key: key);
  13. @override
  14. State<AnimationForRotation> createState() => _AnimationForRotation();
  15. }
  16. class _AnimationForRotation extends State<AnimationForRotation> with SingleTickerProviderStateMixin {
  17. late AnimationController _controller;
  18. late bool isStop = false;
  19. @override
  20. void dispose() {
  21. // Widget销毁时,必须销毁动画
  22. _controller.dispose();
  23. super.dispose();
  24. }
  25. @override
  26. void initState() {
  27. super.initState();
  28. _controller = AnimationController(duration: const Duration(milliseconds: 600), vsync: this);
  29. _controller.repeat();
  30. }
  31. @override
  32. Widget build(BuildContext context) {
  33. if(widget.play == false){ // 动画暂停
  34. setState(()=>isStop = true);
  35. _controller.stop();
  36. }else if(isStop == true){ // 继续动画
  37. setState(()=>isStop = false);
  38. _controller.repeat();
  39. }
  40. return RotationTransition(
  41. alignment: Alignment.center,
  42. turns: _controller,
  43. child: widget.child,
  44. );
  45. }
  46. }