【问题标题】:flutter how to change the color of a button on a click颤动如何在单击时更改按钮的颜色
【发布时间】:2020-09-13 21:32:39
【问题描述】:

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

 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;

},

【问题讨论】:

    标签: flutter colors onpress materialbutton


    【解决方案1】:

    这样试试

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

    【讨论】:

    • 您好,实际上我希望按钮在按下后完全改变颜色,而不是闪现,谢谢您的帮助
    【解决方案2】:

    做到这一点的方法是使用状态。您应该做的第一件事是将您的小部件转换为stateful widget

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

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

    【讨论】:

      猜你喜欢
      • 2021-05-20
      • 2021-07-21
      • 2019-05-20
      • 1970-01-01
      • 2021-10-18
      • 1970-01-01
      • 2021-07-07
      • 1970-01-01
      • 2012-12-06
      相关资源
      最近更新 更多