animated_rotation_widget.dart 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. class AnimatedRotationWidget extends StatefulWidget {
  4. final VoidCallback onPressed;
  5. final ValueChanged<bool>? onHover;
  6. final Widget child;
  7. final RxBool? spinning;
  8. const AnimatedRotationWidget(
  9. {super.key,
  10. required this.onPressed,
  11. required this.child,
  12. this.spinning,
  13. this.onHover});
  14. @override
  15. State<AnimatedRotationWidget> createState() => AnimatedRotationWidgetState();
  16. }
  17. class AnimatedRotationWidgetState extends State<AnimatedRotationWidget> {
  18. double turns = 0.0;
  19. @override
  20. void initState() {
  21. super.initState();
  22. widget.spinning?.listen((v) {
  23. if (v && mounted) {
  24. setState(() {
  25. turns += 1;
  26. });
  27. }
  28. });
  29. }
  30. @override
  31. Widget build(BuildContext context) {
  32. return AnimatedRotation(
  33. turns: turns,
  34. duration: const Duration(milliseconds: 200),
  35. onEnd: () {
  36. if (widget.spinning?.value == true && mounted) {
  37. setState(() => turns += 1.0);
  38. }
  39. },
  40. child: InkWell(
  41. onTap: () {
  42. if (mounted) setState(() => turns += 1.0);
  43. widget.onPressed();
  44. },
  45. onHover: widget.onHover,
  46. child: widget.child));
  47. }
  48. }