颤动如何在单击时更改按钮的颜色



当我单击按钮时,我正在尝试更改按钮的颜色。你能帮我吗,因为我真的做不到。谢谢。

Container(
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new MaterialButton(
child: new Text("1"),
color: Colors.greenAccent,
splashColor: Colors.red,
onPressed:  (){
test=0;
test=1;

},


),
new MaterialButton(
child: new Text("2"),
color: Colors.greenAccent,
onPressed: (){
test=0;
test=2;
},

像这样尝试

Color mySplashColor=Colors.blue; //define in build function or state class
splashColors: mySplashColor,
onPressed(){
setState(){
splashColors=Colors.red;
}
}

执行此操作的方法是使用状态。您应该做的第一件事是将小部件转换为有状态的小部件。

之后,您将一个名为buttonColor的类型状态变量设置为默认值"Colors.greenAccent"。然后,将材质按钮颜色属性设置为此变量。

现在唯一要做的就是使用 (( => setState((( => buttonColor = Colors.red ( 作为按钮 onPressed 属性。

最新更新