change color of the Flutter's FlatButton onPressed

5,254

Solution 1

Your code is perfect but i don't know where you declare your stopSelling variable but i am pretty sure you have declared stopSelling inside the build() method so then you have to declare stopSelling variable outside of the build() method and inside of the class(statefull or stateless).

And It's flutter life cycle rules that when setState() is called then at that time build() method called automatically and it will effect your variable as well as before.

Solution 2

try this....

Container(
      padding: EdgeInsets.symmetric(horizontal: 15,vertical: 15),
      alignment: Alignment.bottomCenter,
      child: SizedBox(
          width: double.infinity, //Full width
          height: 40,
          child: stopSelling? FlatButton(
            child: Text( stopSelling ? "Dejar de vender" : "Empezar a vender",style: TextStyle(fontSize: 20,fontWeight: FontWeight.w300),),
            onPressed: () {
              setState(() {
                stopSelling = !stopSelling;
              });
            },
            textColor: Colors.white,
            color:  Colors.red,
            shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
          ):FlatButton(
            child: Text( stopSelling ? "Dejar de vender" : "Empezar a vender",style: TextStyle(fontSize: 20,fontWeight: FontWeight.w300),),
            onPressed: () {
              setState(() {
                stopSelling = !stopSelling;
              });
            },
            textColor: Colors.white,
            color: Colors.green,
            shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
          ),
      ),
    )
Share:
5,254
Andhire
Author by

Andhire

Updated on December 15, 2022

Comments

  • Andhire
    Andhire over 1 year

    I want to change the color and text of the button when i click on it. But it doesnt change. I change my variable in setState and with the ternary operator set the text and color. I hope you can help guys.

    Container(
         padding: EdgeInsets.symmetric(horizontal: 15,vertical: 15),
         alignment: Alignment.bottomCenter,
         child: SizedBox(
                width: double.infinity, //Full width
                height: 40,
                child: FlatButton(
                    child: Text( stopSelling ? "Dejar de vender" : "Empezar a vender",style: TextStyle(fontSize: 20,fontWeight: FontWeight.w300),),
                    onPressed: () {
                        setState(() {
                          stopSelling = !stopSelling;
                        });
                      },
                    textColor: Colors.white,
                    color: stopSelling?Colors.red:Colors.green,
                    shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
                )
         ),
       ),
    
    • Viren V Varasadiya
      Viren V Varasadiya over 4 years
      Where did you initialize your stopSelling variable ?
  • Andhire
    Andhire over 4 years
    Thanks. That was my mistake.