import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class VideoButton extends HookWidget {
  final bool isPlay;
  final Color? actionColor;
  final Color color;
  final Duration? duration;
  const VideoButton(
      {required this.isPlay, this.actionColor, this.color = Colors.white, this.duration = const Duration(milliseconds: 200), super.key});

  @override
  Widget build(BuildContext context) {
    var icon = useState(isPlay ? AnimatedIcons.pause_play : AnimatedIcons.play_pause);
    var controller = useAnimationController(duration: duration);
    var isPlayState = useState<bool>(isPlay);
    useValueChanged<bool, void>(isPlay, (oldVal, __) {
      if (controller.isAnimating) {
        controller.stop();
        return;
      }
      controller.isCompleted ? controller.reverse() : controller.forward();

      isPlayState.value = isPlay;
    });

    useEffect(() {
      // debugPrint("进入这里了 ${isPlayState.value} 初始值:$isPlay");

      return () {};
    }, []);

    return AnimatedIcon(
      color: isPlayState.value ? (actionColor ?? Theme.of(context).primaryColor) : color,
      icon: icon.value,
      // icon: AnimatedIcons.play_pause,
      progress: controller, // 固定动画状态
      size: 50.0.r,
    );
  }
}
