Search code examples
flutterdartlottie

How to play lottie animation half?


Help me! I want mute and unmute button with Lottie animation. But this mute.json json animation have both. So I need one click play Lottie half animation like this. When clicked Mute and Unmute

 @override
  void initState() {
    super.initState();
    _controller = AnimationController(vsync: this)
      ..value = 0.5
      ..addListener(() {
        setState(() {
          // Rebuild the widget at each frame to update the "progress" label.
        });
      }); 
  }




     Column(
          children[
                   Lottie.asset( 
                                  controller: _controller,
                                  'assets/mute.json', 
                                  animate: true,
                                  onLoaded: (composition) {
                                       setState(() { 
                                      _controller.duration = composition.duration;
                                    });
                                  },
                                ),
],
),

Solution

  • bool mute = false;
    
    @override
      void initState() {
        super.initState();
                                                      // add duration
        _controller =  AnimationController(vsync: this, duration: Duration(milliseconds: 300));
    
      }
    

    use animateTo method on controller.

    InkWell(
                onTap: () {
                  mute = !mute;
                  log(mute.toString());
                  if (mute) {
                    _controller.animateTo(0.5);
                  } else {
                    _controller.animateTo(0);
                  }
                },
                child: LottieBuilder.network(
                  "https://maxst.icons8.com/vue-static/landings/animated-icons/icons/no-sound/no-sound.json",
                  controller: _controller,
                  height: 200,
                ),
              ),