Flutter-如何根据if和else语句的值在点击时切换RaisedButton的颜色

问题描述 投票:0回答:1

我正在创建徽标测验,如果用户选择正确的答案,则按钮的颜色在按下时将变为绿色,否则将变为红色。我尝试使用问题Flutter - How do I toggle the color of a RaisedButton upon click?解决此问题,但并没有太大帮助,对于下一个问题(我所要查找的内容),颜色在印刷时将保持不变。请帮助。

var pressAttention = false;
Padding(
                    padding: const EdgeInsets.only(bottom: 10.0),
                    child: ButtonTheme(
                      minWidth: 300.0,
                      height: 50.0,
                      child: RaisedButton(
                        child: Text(
                          multiChoice[count][0],
                          style: TextStyle(
                            fontSize: 20,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                        color: Colors.white,
                        shape: RoundedRectangleBorder(
                          borderRadius: new BorderRadius.circular(30.0),
                          side: BorderSide(color: Colors.black),
                        ),
                        onPressed: () {
                          setState(() {
                            // checking if the user got the right answer
                            if (correctAnswer[count] == 0) {
                              correctAnswerCount++;
                              // do something to change the color of the button to green
                              // pressAttention = !pressAttention
                            }else{
                              // do something to change the color of the button to red
                            }
                          });
                          nextQuestion();
                        },
                        highlightColor: pressAttention ? Colors.green : Colors.red, //changing to green or red depending on the if and else statement,
                        hoverColor: pressAttention ? Colors.green : Colors.red, //changing to green or red depending on the if and else statement,
                      ),
                    ),
                  ),
flutter button dart
1个回答
0
投票

使用列表存储答案,并检查答案是否与用户答案匹配。如果已设置,则仅设置颜色。

当用户更改问题或回答问题时设置颜色。

© www.soinside.com 2019 - 2024. All rights reserved.