How to play lottie animation for specific time duration in flutter

2,788

Okay you could try directly editing the controller instead, so you're initState will look like this:

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(vsync: this);
    _controller.addListener(() {
      print(_controller.value);
    //  if the full duration of the animation is 8 secs then 0.5 is 4 secs
      if (_controller.value > 0.5) {
// When it gets there hold it there.
        _controller.value = 0.5;
      }
    });
  }

and then your lottie widget looks like this:

  Lottie.asset( 
      'asset/path.json', 
       controller: _controller, 
       onLoaded: (comp){
          _controller
          ..duration = comp.duration
          ..forward(); 
    })

----------- Previous answer below -----------

haven't had a chance to check this but try adding a controller to your animation and using animateTo on the onLoaded.

 Lottie.asset( 
  'asset/path.json', 
   controller: _controller, 
   onLoaded: (comp){
      _controller.animateTo(frameNumber); 
})
Share:
2,788
Aayush Shah
Author by

Aayush Shah

Student at Nirma University

Updated on December 30, 2022

Comments

  • Aayush Shah
    Aayush Shah over 1 year

    I want to play lottie animation for specific time duration in flutter. I don't want to play it by fast forwarding. Like If my animation contains total 90 frames then If I only want to play first 30 frames so can I achieve this? or like If my animation has total 2 seconds but I only want to play 1st second. then How can I do this with using animation controller or any other thing?

  • Aayush Shah
    Aayush Shah almost 3 years
    Thanks for the answer, but I had already tried this before but it is not working as I expected. Specifically, I have a animation which is of total of 90 frames but only first 30 frames are moving. the last 60 frames are steady which I don't want in my app because I am also reversing that animation after the animations gets completed. but as it has 60 frames steady, I get a lag of 1 second. so I want to remove that lag of steady frames. Please share If you have any other alternative. Thanks in advance :)
  • Tyrell James
    Tyrell James almost 3 years
    You could try adding a listener to your controller and pausing it there. I'll post a new answer below.
  • Aayush Shah
    Aayush Shah almost 3 years
    Awesome !!!! Thank you so much :) It works perfectly 🔥