繁体   English   中英

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

[英]flutter how to change the color of a button on a click

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

 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;
}
}

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

之后,您将名为 buttonColor 的 Color 类型的 state 变量设置为具有默认值“Colors.greenAccent”。 然后将 MaterialButton 颜色属性设置为此变量。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM