Listen for an animation to complete
Asked Answered
D

4

11

I'm trying to perform an action after my animation finishes. I tried adding a statusListener but that is not working for me. My code looks like this:

  @override
  void initState() {
    super.initState();
    _controller = new AnimationController(
      duration: new Duration(milliseconds: 500),
      vsync: this,
    )..addStatusListener((AnimationStatus status) {
      print("Going");
      if (status.index == 3 && spins > 0) { // AnimationStatus index 3 == completed animation
        _controller.duration = new Duration(milliseconds: speed - 50);
        _controller.forward(from: _controller.value == null ? 0.0 : 1 - _controller.value);
        spins--;
        print(speed);
      }
    });
  }

The print(Going); never gets executed but my animation does end. What is going wrong?

///---Edit---///

I'm using an AnimatedBuilder, that part of the code looks like this:

child: new AnimatedBuilder(
  animation: _controller,
  child: new Image.network(widget.url),
  builder: (BuildContext context, Widget child) {
    return new Transform.rotate(
      angle: _controller.value * 2.0 * math.PI,
      child: child,
    );
  },
),
Darnelldarner answered 26/7, 2017 at 3:21 Comment(0)
B
14

Reacting to your comment and edit I looked into the AnimationBuilder. Adapting the example in the docs I came up with this working solution:

class Spinner extends StatefulWidget {
  @override
  _SpinnerState createState() => new _SpinnerState();
}

class _SpinnerState extends State<Spinner> with SingleTickerProviderStateMixin {
  AnimationController _controller;
  CurvedAnimation _animation;

  @override
  void initState() {
    super.initState();
    _controller = new AnimationController(
      duration: const Duration(seconds: 5),
      vsync: this,
    )..forward();

    _animation = new CurvedAnimation(
        parent: _controller,
        curve: Curves.linear,
    )..addStatusListener((AnimationStatus status) {
      if (status == AnimationStatus.completed)
        print('completed');
    });
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return new AnimatedBuilder(
      animation: _animation,
      child: new Container(width: 200.0, height: 200.0, color: Colors.green),
      builder: (BuildContext context, Widget child) {
        return new Transform.rotate(
          angle: _controller.value * 2.0 * 3.1415,
          child: child,
        );
      },
    );
  }
}

As you can see, I used the controller as parent to an animation, which was than used as animation for the AnimationBuilder. Hope it helps.

Bechuana answered 26/7, 2017 at 15:54 Comment(0)
I
5

You can also use the whenComplete listener

_animationController.forward().whenComplete((){
     //Trigger different responses, when animation is started at different places.
})
Interrogator answered 12/12, 2020 at 5:3 Comment(0)
B
4

Following the example in the flutter gallery's progress indicators you should attach the StatusListener to an animation, not the controller

_controller = AnimationController(
  duration: const Duration(milliseconds: 1500),
  vsync: this,
)..forward();

_animation = CurvedAnimation(
  parent: _controller,
  curve: const Interval(0.0, 0.9, curve: Curves.fastOutSlowIn),
  reverseCurve: Curves.fastOutSlowIn
)..addStatusListener((AnimationStatus status) {
  if (status == AnimationStatus.dismissed)
    _controller.forward();
  else if (status == AnimationStatus.completed)
    _controller.reverse();
});

I have not tested this with your code. Just shout, if it doesn't work ;)

Bechuana answered 26/7, 2017 at 8:17 Comment(1)
I forgot telling that I am using an AnimatedBuilder (I edited my post and added some code). I don't think I can apply your method to what I've written, any other ideas?Darnelldarner
G
0

In the scenario, when developers utilize an AnimationController for a countdown timer, they may observe that AnimationStatus.dismissed is triggered when the timer reaches zero, instead of AnimationStatus.completed being called as expected.

Gamache answered 15/5 at 11:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.