Home > Net >  Flutter How to stop the LinearProgressIndicator when it reaches the end
Flutter How to stop the LinearProgressIndicator when it reaches the end

Time:12-08

I need the LinearProgressIndicator to stop when it reaches the end instead of repeating, i gave the duration for example 10 secs, and i need that when the LinearProgressIndicator reaches the end of the seconds to stop.

late AnimationController controller;

  void initState() {
    super.initState();
    futureData = fetchData1() as Future<List<QuizInfo>?>;

    controller = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 5),
    )..addListener(() {
        setState(() {});
      });
    controller.repeat(reverse: false);
    super.initState();
  }



  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

child: Container(
                        height: 7,
                        child: RotatedBox(
                          quarterTurns: -2,
                          child: LinearProgressIndicator(
                            value: controller.value,
                            color: Colors.white,
                            backgroundColor: Colors.yellow,
                          ),
                        ),
                      ),

CodePudding user response:

Found the answer. So you need to use the method controller.animateTo(//seconds of the duration):

  late AnimationController controller;

  void initState() {
    super.initState();
    futureData = fetchData1() as Future<List<QuizInfo>?>;

    controller = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 5),
    )..addListener(() {
        setState(() {});
      });
    controller.animateTo(5.0);
    super.initState();
  }



  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  child: Container(
                 height: 7,
                 child: RotatedBox(
                          quarterTurns: -2,
                          child: LinearProgressIndicator(
                            value: controller.value,
                            color: Colors.white,
                            backgroundColor: Colors.yellow,
                          ),
                        ),
               ),

CodePudding user response:

You have to listen to the events of your animation in order to make a decision I believe that we have already answered that,Click here

CodePudding user response:

You can use a TweenAnimationBuilder to easily achieve that.

  TweenAnimationBuilder(
    tween: Tween(end: 0.0), // change this from 0.0 to 1.0 and hot reload
    duration: const Duration(seconds: 2),
    builder: (BuildContext context, double value, Widget? child) {
      return LinearProgressIndicator(value: value);
    },
  )

So you can pass a variable to end param to control when to start the counter, for example declare bool counting = false and then pass counting ? 0.0 : 1.0 to it.

  • Related