【问题标题】:How to change color for unique RaisedButton?如何更改独特 RaisedButton 的颜色?
【发布时间】:2019-09-08 15:11:06
【问题描述】:

我有一个字符串列表:

List _choice = ["Europe","Asia","Africa"];

对于我生成 RaisedButton 的每个值:

我还有一个变量:

字符串答案 = “欧洲”;

我的目标: 如果用户单击任何 Raisedbutton,则将 Text 值等于 answer 值的颜色更改为绿色。对于其他文本值不等于答案值的情况,将颜色更改为红色。

我的代码的问题是所有 RaisedButton 都变为红色或绿色。

 Widget build(BuildContext context) {
    List<String> _choice = <String>["Europe","Asia","Africa"];
    String answer = "Europe";
    int NumberChoice = _choice.length;

    //RaisedButtons
                        for (var i = 0; i < NumberChoice; i++)
                          new RaisedButton(
                            color: _colorButton,
                            child: new Text(
                              _choice[i],
                              style: new TextStyle(
                                  fontSize: 20.0, color: Colors.white),
                            ),
                            onPressed: () {
                              setState(() {
                                if (_choice[i] == answer) {
                                  print("--------------- Corect ----------------");
                                  _colorButton = Colors.greenAccent;
                                  scoreResult = 1;
                                } else {
                                  print("--------------- False ------------------------");
                                  _colorButton = Colors.redAccent;
                                  scoreResult = 0;
                                }
                              });
                            },
                          ),

【问题讨论】:

  • 你把_colorButton给了所有的RaisedButton,这就是为什么......,你应该根据索引给不同的颜色。
  • 谢谢,是否可以为每个 RaisedButton 提供一个唯一的 ID,例如 RaisedButton_1、RaisedButton_2、...?
  • 我已经回答了你的问题@ThibautFAURE。让我知道这是否适合你。谢谢
  • 嘿@ThibautFAURE,我找到了解决方案,请在下面找到已编辑的答案。它现在可以工作了。请给它竖起大拇指并标记为正确。将等待得到您的回复。 :)

标签: flutter dart


【解决方案1】:

我终于解决了你的案子。这需要付出很多努力,但最终,我们做到了。

我所做的是,我在按下按钮时重建了小部件并更改了 RaisedButton() 的颜色键本身的颜色

我知道这听起来有点复杂,但在研究了解决方案后,你会明白的。

在转向解决方案之前,这里是演示。按钮默认是粉红色的,只要我点击任何按钮,结果就会出现,错误答案=红色,正确=绿色

class RadioButtonPage extends StatefulWidget {
  @override
  RadioButtonPageState createState() => RadioButtonPageState();

}

class RadioButtonPageState extends State<RadioButtonPage> {

  List<String> _choice = ["Europe","Asia","Africa"];
  String answer = "Europe";
  var score;

  //this bool does the real magic since you want the result to be seen on the press of the button, so here you go, this keeps track of the pressed button whether it is pressed once or not
  bool isPressedOnce = false;

  List<Widget> getButtons(){
    List<Widget> _listWidget = [];
     for(int i=0; i< _choice.length; i++){
      _listWidget.add(
        RaisedButton(
           //Everytime it builds up, it will work according to this situation, hence works
          color: isPressedOnce ? (_choice[i] == answer ? Colors.greenAccent : Colors.red) : Colors.pink,
          child: Text(_choice[i]),
          onPressed: (){
            //Changes the state of bool to true, since any button is pressed
            setState(() {
              this.isPressedOnce = true;
              if(_choice[i] == this.answer) score = 1;
              else score = 0;
            });

            //Rebuilding the widget again, and now with the current condition I have put on that color key,  it will finally work as per the requirements
            this.getButtons();
          }
        )
      );
    }

    return _listWidget;
  }

  @override
  Widget build(BuildContext context) {
   return Scaffold(
     appBar: AppBar(
        title: Text('Welcome'),
     ),
     body: Center(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: this.getButtons()
       )
     )
   );
  }
}

【讨论】:

  • 我已经使用了 setState(),为了达到同样的效果,我们必须做一些类似于 @Eugene 的事情。如果您有其他解决方案,请告诉我。我会很感激向你学习。谢谢。但不要投反对票,这对他来说是一种解决方法,因为没有人帮助他回答这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-27
  • 1970-01-01
  • 2019-07-03
  • 1970-01-01
  • 2020-06-05
  • 2018-11-24
  • 1970-01-01
相关资源
最近更新 更多