Giuliano Condrache
Giuliano Condrache

Reputation: 300

How to add delay between each repeat of animation in flutter

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,
      ),
    ),
  ),
);


   

Upvotes: 5

Views: 2324

Answers (2)

Mehdi
Mehdi

Reputation: 170

delay after each repeat :

  @override
  void initState() {// <-- add initstate
    super.initState();
    _animationController.forward(from: 0.0);
  }

  late final AnimationController _animationController = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 1))
     //..repeat(reverse: true); // <-- comment this line

    ..addStatusListener((AnimationStatus status) { // <-- add listener
      if (status == AnimationStatus.completed) {
        Future.delayed(
            Duration(milliseconds: _animationController.value == 0 ? 500 : 0),
            () {
          _animationController
              .animateTo(_animationController.value == 0 ? 1 : 0);
        });
      }
    });

Upvotes: 0

Md. Yeasin Sheikh
Md. Yeasin Sheikh

Reputation: 63809

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();
      }
    });
  }

Upvotes: 6

Related Questions