Search code examples
flutterflutter-animation

AnimatedContainer onTap animate once forward and back


https://api.flutter.dev/flutter/widgets/AnimatedContainer-class.html

The example flutter.dev takes one tap to animate to a different look, and one tap to animate back to the original. I was wondering, what if I want it to animate once forward, hold one second, and then backwards on one tap. Can I use a Timer to achieve this or should I be using AnimationController instead?


Solution

  • AnimatedContainer provides onEnd, you can revese the animation from it.

    child: AnimatedContainer(
      onEnd: () async {
        await Future.delayed(Duration(seconds: 1));
        setState(() {
          selected = false;
        });
      },
    

    Also, you can use AnimationController with listener.