Home > front end >  AnimatedContainer onTap animate once forward and back
AnimatedContainer onTap animate once forward and back

Time:02-14

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?

CodePudding user response:

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.

CodePudding user response:

The best choice would be using the AnimationController since it gives you more control over the object. One more important thing about using an AnimationController instead of a Timer is that the controller uses TickerProvider class for a more smooth start and ending animation.

  • Related