我希望我的按钮(白色)在用户选择正确答案时为绿色,在用户选择错误时为红色,然后选择正确的绿色 [英] I want my buttons (white color) to green if the user picks the correct answer and red if wrong, then the correct one in green

查看:42
本文介绍了我希望我的按钮(白色)在用户选择正确答案时为绿色,在用户选择错误时为红色,然后选择正确的绿色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RaisedButton (
  padding: EdgeInsets.fromLTRB(10.0, 8.0, 10.0, 8.0),
  elevation: 8.0,
  color: butColor ? Colors.green : Colors.red,
  shape: RoundedRectangleBorder(
      borderRadius: BorderRadius.circular(10)),
  onPressed: () {
    setState(() {
      if (quiz.choices[questionNumber][0] == quiz.correctAnswers[questionNumber]){
        finalScore = finalScore + 4;
        butColor = true;

      }else{
        debugPrint("Wrong");
        butColor = false;
      }

    });
  },
  child: Text(quiz.choices[questionNumber][0],
  style: TextStyle(
    fontSize: 22.0,
    fontFamily: "AlegreyaSans",
    color: Colors.black
  ),),
),

我有bool变量 butColor 也设置为 false

I have bool variable butColor set to false too.

当我尝试将按钮颜色设置为白色并在if else中执行switch语句时,我仍然只得到白色按钮。

When I try setting the buttons color to white and doing the switch statement inside the if else, I still get just white color buttons.

推荐答案

您可以简单地设置一个Enum,然后根据值更改按钮的颜色。只需创建一个变量来保存枚举值即可。我在这里使用了buttonStatus变量并将其初始化为ButtonStatus.Unanswered

You can simply set an Enum and according to the value change the color of the button. Just create a variable to hold the enum value. I have used buttonStatus variable here and initialize it to ButtonStatus.Unanswered

 enum ButtonStatus{Unanswered,CorrectAnswer,WrongAnswer}

    Color getButtonColor (ButtonStatus status) {
      switch (status) {
        case ButtonStatus.Unanswered: {
          return Colors.white;
        }
        break;
        case ButtonStatus.CorrectAnswer: {
          return Colors.green;
        }
        break;
        case ButtonStatus.WrongAnswer: {
          return Colors.red;
        }
        break;

        default: {
          return Colors.white;
       }
     }
   }

然后您的代码将是更新如下

And then your code will be updated as follows

RaisedButton (
    padding: EdgeInsets.fromLTRB(10.0, 8.0, 10.0, 8.0),
    elevation: 8.0,
    color: getButtonColor(buttonStatus),
    shape: RoundedRectangleBorder(
    borderRadius: BorderRadius.circular(10)),
    onPressed: () {
        setState(() {
            if (quiz.choices[questionNumber][0] == 
                 quiz.correctAnswers[questionNumber]){
                    finalScore = finalScore + 4;
                    buttonStatus = ButtonStatus.CorrectAnswer;
                 }else{
                    debugPrint("Wrong");
                    buttonStatus = ButtonStatus.WrongAnswer
                }
            });
           },
    child: Text(quiz.choices[questionNumber][0],
               style: TextStyle(
               fontSize: 22.0,
               fontFamily: "AlegreyaSans",
               color: Colors.black
             ),
         ),
     ), 

希望这就是您想要的。

这篇关于我希望我的按钮(白色)在用户选择正确答案时为绿色,在用户选择错误时为红色,然后选择正确的绿色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆