flutter - 如何在 Flutter 的下一页上将计数增加一?
问题描述
我正在 Flutter 中创建一个测验应用程序,并尝试按该顺序显示问题页面、答案页面和问题页面。当页面从答案页面转换到问题页面时,我想显示下一个问题,但我无法正确传递增量值。我尝试使用 Navigator.push() 并将以下 setsetState((){}) 作为要转换的页面的参数,但出现错误。你能给我一些建议吗?谢谢你。
answer_page.dart:
import 'package:flutter/material.dart';
import '../components/next_question_button.dart';
import '../screens/question_page.dart';
import '../quiz_list.dart';
class CorrectAnswerPage extends StatelessWidget {
CorrectAnswerPage({this.scoreKeeper});
final List<Icon> scoreKeeper;
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Container(
child: Center(
child: Text(
'correct',
style: TextStyle(
fontSize: 100,
fontWeight: FontWeight.bold,
color: Colors.red),
),
),
),
Divider(
color: Colors.grey,
indent: 16,
endIndent: 16,
),
Text('EXPLANATION',
style: TextStyle(
fontSize: 20, fontWeight: FontWeight.bold, color: Colors.grey)),
NextQuestionButton(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => QuestionPage(setState: () {
quizList.nextQuestion();
})));
},
),
Row(
children: scoreKeeper,
)
],
);
}
}
question_page.dart:
import 'package:flutter/material.dart';
import '../screens/wrong_answer_page.dart';
import '../components/answer_button.dart';
import '../quiz_list.dart';
import 'correct_answer_page.dart';
QuizList quizList = QuizList();
class QuestionPage extends StatefulWidget {
QuestionPage({@required this.setState});
final setState;
//QuestionPage(void nextQuestion);
// QuestionPage(this.scoreKeeper);
//
// List<Icon> scoreKeeper;
@override
_QuestionPageState createState() => _QuestionPageState();
}
class _QuestionPageState extends State<QuestionPage> {
List<Icon> scoreKeeper = [];
void checkAnswer(String userPickedAnswer) {
String correctAnswer = quizList.getQuestionAnswer();
if (userPickedAnswer == correctAnswer) {
scoreKeeper.add(
Icon(Icons.circle_outlined, color: Colors.red),
);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CorrectAnswerPage(
scoreKeeper: scoreKeeper)));
} else {
scoreKeeper.add(
Icon(Icons.close, color: Colors.blue),
);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => WrongAnswerPage(
scoreKeeper: scoreKeeper)));
}
setState(() {
quizList.nextQuestion();
});
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
flex: 5,
child: Padding(
padding: EdgeInsets.all(10.0),
child: Center(
child: Text(
quizList.getQuestionText(),
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 25.0,
color: Colors.black,
),
),
),
),
),
Expanded(
child: Padding(
padding: EdgeInsets.all(15.0),
child: AnswerButton(
userPickedAnswer: 'a',
onTap: () {
setState(() {
checkAnswer('a');
});
}),
)),
Expanded(
child: Padding(
padding: EdgeInsets.all(15.0),
child: AnswerButton(
userPickedAnswer: 'b',
onTap: () {
setState(() {
checkAnswer('b');
});
}),
)),
Expanded(
child: Padding(
padding: EdgeInsets.all(15.0),
child: AnswerButton(
userPickedAnswer: 'c',
onTap: () {
setState(() {
checkAnswer('c');
});
}),
)),
Expanded(
child: Padding(
padding: EdgeInsets.all(15.0),
child: AnswerButton(
userPickedAnswer: 'd',
onTap: () {
setState(() {
checkAnswer('d');
});
}),
)),
//TODO: Add a Row here as your score keeper
Row(children: this.scoreKeeper)
],
);
}
}
解决方案
QuestionPage(setState: () {
quizList.nextQuestion();
})));
在这里你传递一个函数。随着() {}
您创建一个函数并将其传递到下一页。
简单地:
QuestionPage(setState: quizList.nextQuestion());
然后下一个问题将存储在您的setState
变量中。
开始声明变量的类型,这样您就不会遇到尝试传递对象但传递函数的问题,并且 IDE 什么也没说,因为它希望您知道自己在做什么:
final Question setState;
推荐阅读
- javascript - 由于背景图像,滚动条消失
- php - 如何修复 wp_remote_get() 错误 // 从数组中获取值?
- join - CouchDB 中跨多种文档的复杂查询
- c# - 为什么 Guid 在 Azure 表存储中存储为字符串而不是 guid 类型?
- angularjs - 无法使用 Angular.js 更改标签的 href 值
- c# - 如何在 C# 中使用 Microsoft Print to PDF 将 DOCX 转换为 PDF?
- python - 我如何使用 python 脚本来做,就好像我在使用 shell 一样?
- java - 从控制器中删除 @ResponseBody 时,收到 404 not found 错误消息
- spring-security-ldap - 如何使用 Spring Security 从 LDAP 获取用户角色
- javascript - 如何设置已设置为变量的参数?