Home > Back-end >  Multiple setState flutter only updating with the first one only
Multiple setState flutter only updating with the first one only

Time:11-09

Below is my code:
The issue I'm facing => first button updating the second button state as well.

But it should be working differently:
first with just first,
second with just second

I just want to achieve the state of the second button as independent from others what should i need to update thankyou, any suggestion will be great for me

class _LinearProgressBar extends State<LinearProgressBar> {
  bool loadingLinear = false;
  bool loadingCircular = false;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            appBar: AppBar(
              title: const Text("CheckBox and Radio Button Stateful"),
            ),
            body: Center(
              child: Column(
                children: [
                  Container(
                    child: loadingLinear
                        ? const LinearProgressIndicator()
                        : const Text("Click to download "),
                  ),

           //Here is my first button which reflects the second button state as well
                  ElevatedButton(
                    style: ElevatedButton.styleFrom(
                      primary: Colors.red, // background
                      onPrimary: Colors.white, // foreground
                    ),
                    onPressed: () {
                      setState(() {
                       // when I click here it also updates the second set state of SecondBtn 
                        loadingLinear = !loadingLinear;
                      });
                    },
                    child: const Text('FirstBtn'),
                  ),
                  Container(
                    child: loadingLinear
                        ? CircularProgressBar_()
                        : const Text("Click to download"),
                  ),

               //   Here is my second button which doesn't work


                  ElevatedButton(
                    style: ElevatedButton.styleFrom(
                      primary: Colors.red, // background
                      onPrimary: Colors.white, // foreground
                    ),
                    onPressed: () {
                      setState(() {
                     //   this state is not working even on clicking...
                        loadingCircular = !loadingCircular;
                      });
                    },
                    child: const Text('SecondBtn'),
                  )
                ],
              ),
            )));
  }
}

CodePudding user response:

I thinks you use the wrong variable on the second container, use this instead

Container(
    child: loadingCircular // not loadingLinear
           ? CircularProgressBar_()
           : const Text("Click to download"),
),

CodePudding user response:

because in second button you change loadingCircular state ,,, while you check on loadingLinear value

  • Related