Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如果用户选择了正确的答案,我希望我的按钮(白色)为绿色,如果选择了错误,则为红色,然后是绿色的正确按钮_Flutter_Dart - Fatal编程技术网

Flutter 如果用户选择了正确的答案,我希望我的按钮(白色)为绿色,如果选择了错误,则为红色,然后是绿色的正确按钮

Flutter 如果用户选择了正确的答案,我希望我的按钮(白色)为绿色,如果选择了错误,则为红色,然后是绿色的正确按钮,flutter,dart,Flutter,Dart,我将bool变量butColor也设置为false 当我尝试将按钮颜色设置为白色并在if-else中执行switch语句时,仍然只会得到白色按钮。您可以简单地设置枚举并根据值更改按钮的颜色。只需创建一个变量来保存枚举值。我在这里使用了buttonStatus变量,并将其初始化为buttonStatus.Unanswered RaisedButton ( padding: EdgeInsets.fromLTRB(10.0, 8.0, 10.0, 8.0), elevation: 8.0,

我将bool变量
butColor
也设置为
false


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

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

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
  ),),
),
然后您的代码将更新如下

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

希望这就是你想要的。

你能给我一个更详细的解释@Phil DubemI希望我的按钮有3种颜色,一种是活动的,一种是if-else语句正确的,另一种是if-false@T.TSageI必须编辑你的switch语句,除此之外,它对我的要求非常有效。谢谢
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
             ),
         ),
     ),