【发布时间】:2021-02-15 15:23:23
【问题描述】:
我正在用颤振构建一个测验应用程序,每次用户选择错误答案时,我希望正确答案(按钮)变为绿色。
这些是我的变量
Color colorToShow = transColor;
Color testColor = transColor;
Color right = correctColor;
Color wrong = redColor;
Map<int, Color> btnColor = {
0: Colors.transparent,
1: Colors.transparent,
2: Colors.transparent,
};
Map<int, Color> tabTextColor = {
0: Colors.blue[800],
1: Colors.blue[800],
2: Colors.blue[800],
};
int questionIndex;
bool disableButton = false;
bool firstPress = true;
这是我检查正确答案的功能:
void checkAnswer1(k) {
if (questionBank[questionIndex]['answers'][k]['correct']) {
colorToShow = right;
Provider.of<QuizData>(context, listen: false).countMarks();
} else {
colorToShow = wrong;
if (questionBank[questionIndex]['answers'][k + 1]['correct']) {
testColor = right;
}
Provider.of<QuizData>(context, listen: false)
.addWrongAnswers(questionIndex);
print(Provider.of<QuizData>(context, listen: false).wrongAnswers);
}
setState(() {
tabTextColor[k] = Colors.white;
btnColor[k] = colorToShow;
btnColor[k + 1] = testColor;
disableButton = true;
firstPress = false;
});
}
这里是带有输入的按钮。
AnswerTab(
questionBank[questionIndex]['answers']
[0]['text'],
firstPress
? () => checkAnswer1(0)
: null,
tabTextColor[0],
btnColor[0]),
SizedBox(height: 16),
AnswerTab(
questionBank[questionIndex]['answers']
[1]['text'],
firstPress
? () => checkAnswer1(1)
: null,
tabTextColor[1],
btnColor[1]),
SizedBox(height: 16),
AnswerTab(
questionBank[questionIndex]['answers']
[2]['text'],
firstPress
? () => checkAnswer1(2)
: null,
tabTextColor[2],
btnColor[2]),
这是我的题库的一个sn-p
final List<Map<String, dynamic>> questionBank = [
{
'questionText':
"Το ψαροντουφεκο στη περιοχή του Κεντρικού Λιμεναρχείου Πειραιά απαγορεύεται:",
'answers': [
{'text': "Έξω από το λιμάνι του Πειραιά.", "correct": false},
{'text': "Στις πλάζ του Πειραιά.", "correct": false},
{
'text': "Από την Πειραϊκή Χερσόνησο κι έως το 42 χιλ. Πειραιά Σουνίου.",
"correct": true
},
]
},
我知道我的问题在于函数和 If 语句,但我想不出办法。 现在我已经测试检查是否只有下一个按钮 (k+1) 正确变绿,当我选择错误答案时它会变绿,但当我选择正确答案时它也会变绿。
请帮忙!
【问题讨论】:
-
什么是
testColor? -
我在第二个按钮上使用的第二个颜色变量。 (选中的按钮之后的下一个按钮 -> k + 1)
标签: function flutter if-statement dart