rrousselGit / flutter_hooks

React hooks for Flutter. Hooks are a new kind of object that manages a Widget life-cycles. They are used to increase code sharing between widgets and as a complete replacement for StatefulWidget.
MIT License
3.07k stars 175 forks source link

animation value was not got as expect when use `controller.reverse()` #355

Closed trevorwang closed 1 year ago

trevorwang commented 1 year ago

Describe the bug

the animation.value was not get as expected when reverse() was called. The value should change smoothly from 1 to 0. But currently the got only limited values which are closed to 1

Screenshot 2023-05-04 at 15 18 51

To Reproduce

class DemoAnimation extends HookWidget {
  const DemoAnimation({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final ticker = useSingleTickerProvider();
    final controller = useAnimationController(
      vsync: ticker,
      duration: const Duration(seconds: 1),
    );

    final a = Tween<double>(begin: 0, end: 1).animate(controller)
      ..addStatusListener((status) {
        if (status == AnimationStatus.completed) {
          controller.reverse();
        } else if (status == AnimationStatus.dismissed) {
          controller.forward();
        }
      });

    final o = useAnimation(a);

    controller.forward();

    print("o: $o");
    return Opacity(
      opacity: o,
      child: Container(
        margin: const EdgeInsets.symmetric(vertical: 2),
        width: 6,
        height: 12,
        color: Colors.black,
      ),
    );
  }
}

Expected behavior

animation value would be got as expected.

Screenshot 2023-05-04 at 15 20 43
trevorwang commented 1 year ago

It should be check whether the controller isAnimating before forward

    if (!ac.isAnimating) {
      ac.forward();
    }