MayuriXx
MayuriXx

Reputation: 847

change color RaisedButton onPressed

i want to changed my color when i press my RaisedButton.

So I init a boolean and i call this in my method setState().

class _Poll extends State<PollPage> {
  var pressed = true;

new RaisedButton(
                    color: pressed ? Colors.pink[50] : Colors.pink,
                    hoverColor: Colors.pinkAccent,
                    focusColor: Colors.pinkAccent,
                    child: new Text(choix[3]),
                    onPressed: ((){
                      pressed = !pressed;
                    }),
                  ),

}

But when i clicked on my button, the init color staying.

Upvotes: 0

Views: 120

Answers (1)

roipeker
roipeker

Reputation: 1243

you forgot to call setState().

@override
  Widget build(BuildContext context) {
    return RaisedButton(
      color: pressed ? Colors.blue : Colors.pink,
      hoverColor: Colors.pinkAccent,
      focusColor: Colors.pinkAccent,
      child: new Text("Raised Button"),
      onPressed: () => setState((){pressed = !pressed;}),
    );
  }

Upvotes: 3

Related Questions