How to add delay between each repeat of animation in flutter

113

Just change the duration you want => await Future.delayed(Duration(seconds: 3)); I test it with seconds: 3 to get better idea.

 late final AnimationController _animationController =
      AnimationController(vsync: this, duration: const Duration(seconds: 3))
        ..forward(); 
 @override
  void initState() {
    super.initState();
    _animationController.addListener(() async {
      print(_animationController.status);
      if (_animationController.isCompleted) {
        await Future.delayed(Duration(seconds: 3));
        _animationController.reverse();
      } else if (_animationController.isDismissed) {
        await Future.delayed(Duration(seconds: 3));
        _animationController.forward();
      }
    });
  }
Share:
113
Giuliano Condrache
Author by

Giuliano Condrache

Updated on December 19, 2022

Comments

  • Giuliano Condrache
    Giuliano Condrache 5 months

    I have a SlideTransition with a container in my application, and it repeats forever, but i would like a delay after each repeat. so it would be like this:Visual Representation

    Here's my code

      late final AnimationController _animationController = AnimationController(
        vsync: this,
        duration: const Duration(seconds: 1)
      )..repeat(reverse: true); // Here there should be the 500 millisecond delay
      late final Animation<Offset> _animation = Tween<Offset>(
        begin: Offset.zero,
        end: Offset(0, 1),
      ).animate(_animationController);
    

    . . .

    return Scaffold(
      body: Center(
        child: SlideTransition(
          position: _animation,
          child: Container(
            height: 100,
            width: 100,
            color: Colors.red,
          ),
        ),
      ),
    );
    
  • Giuliano Condrache
    Giuliano Condrache almost 2 years
    this worked but it only added a delay once the repeat has also been done, it is my fault i didn't specify i wanted between reverse and forward aswell