Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeating animations specific times e.g 20 times by Flutter

Tags:

flutter

dart

I have a simple animation :

  Animation<double> animation;
  Tween<double> tween = Tween(begin: 1, end: 1.15);
  AnimationController animationController;
  @override
  void initState() {
    super.initState();
    animationController =
        AnimationController(vsync: this, duration: Duration(milliseconds: 300));
    animation = animationController.drive(tween);
}

What i want ?

make this animation repeats e.g 20 times .

What i tried before posted this question ?

1- This method not have a parameter or way that makes me repeats this animation e.g 20 times.

it repeats and repeats and repeats forever :

    animationController.repeat();

2- Use simple loops . It hangs my app, it requires to stop entire application and rerun it to solve that hanging . It's seems that loops completely not dealing with futures :

                            do {
                              animationController.forward().then((x) {
                                animationController.reverse().then((x) {
                                  repeats++;
                                });
                              });
                            } while (repeats < 20);

3- Making int variable and add a listener ..etc,it's working , but seems that it's not the best way to do that matter :

  int repeats = 0;

    animation.addStatusListener((status) {
      if (repeats < 20) {
        if (status == AnimationStatus.completed) {
          animationController.reverse();
        } else if (status == AnimationStatus.dismissed) {
          animationController.forward();
        }
        repeats++;
      }    });

4- make chain of then . **No comment on it ** 😂😂😂 :

    animationController.forward().then((x) {
      animationController.reverse().then((x) {
        animationController.forward().then((x) {
          animationController.reverse().then((x) {
            animationController.forward().then((x) {
              animationController.reverse();
            });
          });
        });
      });
    });

Now , shortly how can i repeat animation 20 times

like image 678
Mohamed Gaber Avatar asked Dec 23 '19 23:12

Mohamed Gaber


People also ask

What is explicit animation in Flutter?

Explicit animations are a set of controls for telling Flutter how to rapidly rebuild the widget tree while changing widget properties to create animation effects. This approach enables you to create effects that you can't achieve using implicit animations.

How many types of animation are there in Flutter?

Flutter provides two types of techniques for animation. These techniques are: Implicit Animation. Explicit Animation.

What is a refresh rate of your animation in Flutter?

Flutter aims to provide 60 frames per second (fps) performance, or 120 fps performance on devices capable of 120Hz updates. For 60fps, frames need to render approximately every 16ms.

What are the limitations of animation in flutter?

The duration of the animation process affects the speed (slowness or fastness) of the animation. The ability to control the animation process like starting the animation, stopping the animation, repeating the animation to set number of times, reversing the process of animation, etc., In Flutter, animation system does not do any real animation.

Is there a repeat method for animationcontroller?

There is a repeat method for AnimationController. Sorry, something went wrong. This seems to be answered. Sorry, something went wrong. This seems to be answered. Did you find any answer?

What is animation control in flutter?

The ability to control the animation process like starting the animation, stopping the animation, repeating the animation to set number of times, reversing the process of animation, etc., In Flutter, animation system does not do any real animation.

What is call recurring function in flutter Dart repeat method?

The Timer.periodic () methods enable us to call a function or statement after a particular given time again and again. The margin of time is the delay between each calling of statement. So in this tutorial we would learn about Example of Call Recurring Function in Flutter Dart Repeat Method in Android iOS.


Video Answer


3 Answers

You can try it:

5 - Use TickerFuture return from repeat, set timeout then stop animation.

 AnimationController _animationController;

  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(
      vsync: this,
      duration: Duration(seconds: 3),
    );

    _animationController.addListener(() => setState(() {}));
    TickerFuture tickerFuture = _animationController.repeat();
    tickerFuture.timeout(Duration(seconds:  3 * 10), onTimeout:  () {
      _animationController.forward(from: 0);
      _animationController.stop(canceled: true);
    });
  }
like image 96
duongdt3 Avatar answered Oct 22 '22 00:10

duongdt3


This can be easily done without the timer with extension method:

extension on AnimationController {
  void repeatEx({@required int times}) {
    var count = 0;
    addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        if (++count < times) {
          reverse();
        }
      } else if (status == AnimationStatus.dismissed) {
        forward();
      }
    });
  }
}

And you could use it like this:

    _controller = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 1),
    )
      ..repeatEx(times: 10)
      ..forward();
like image 25
Tomek Polański Avatar answered Oct 22 '22 01:10

Tomek Polański


If you have a Widget with a playing argument that can set to true or false dynamically (to start and stop animation dynamically), you can improve behaviour by using a cancellable Timer() instead of Future.timeout :

  void updatePlaying() {
    if (widget.playing != false && !_controller.isAnimating && !_loopHasCompleted) {
      _controller.repeat();
      _timeout?.cancel();
      _timeout = Timer(widget.duration * widget.loops, () {
        _controller.reset();
        _loopHasCompleted = true;
      });
    } else if (widget.playing == false) {
      if (_controller.isAnimating)
        _controller.reset();
      _loopHasCompleted = false;
    }
  }
like image 2
Nicolas Youpi Avatar answered Oct 22 '22 00:10

Nicolas Youpi