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)),
)
),
),
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.