4

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


   
  • Does [this](https://stackoverflow.com/questions/64236824/how-to-add-some-delay-between-animationcontroller-repeat-in-flutter) help you? – Peter Koltai Aug 16 '21 at 18:58
  • not really, i have seen that post before, but the problem is that i don't really know how to integrate the answer in my code. – Giuliano Condrache Aug 16 '21 at 19:28

2 Answers2

4

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();
      }
    });
  }
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
0

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);
        });
      }
    });
Mehdi
  • 170
  • 1
  • 9
  • 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 – Giuliano Condrache Aug 16 '21 at 21:26