我正在尝试提问,但是它并没有随着我setState
的更新而更新。当我按下凸起的按钮时,questionNumber
变量将更新并显示不同的questionText
。但是,即使按下它,文本值也不会更新。
import 'package:flutter/material.dart';
import 'QuestionBrain.dart';
class QuestionPage extends StatefulWidget {
static const pageId = 'QuestionPage';
@override
_QuestionPageState createState() => _QuestionPageState();
}
class _QuestionPageState extends State<QuestionPage> {
@override
Widget build(BuildContext context) {
QuizBrain quizBrain = QuizBrain();
void nextQuestion() {
setState(() {
quizBrain.nextQuestion();
});
}
return SafeArea(
child: Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Center(
child: Text('Questions'),
),
Text(quizBrain.getQuestionText()),
RaisedButton(
onPressed: nextQuestion,
child: Text('Next Qns'),
),
],
),
));
}
}
这是我的QuestionBrain
:
import 'Questions.dart';
class QuizBrain {
int _questionBankNumber = 0;
List<Question> _questionBank = [
Question('Why is the world round?', 'Dummy answer1', 'Dummy answer2',
'Dummy answer3', 'Dummy answer4', 'Dummy answer1'),
Question('Is the sun blue in colour?', 'Dummy answer1', 'Dummy answer2',
'Dummy answer3', 'Dummy answer4', 'Dummy answer1'),
Question('How often does the rooster moo?', 'Dummy answer1',
'Dummy answer2', 'Dummy answer3', 'Dummy answer4', 'Dummy answer1')
];
void nextQuestion() {
if (_questionBankNumber < _questionBank.length - 1) {
_questionBankNumber++;
}
}
}
答案 0 :(得分:1)
我认为您可以如下更改对象位置:-
class _QuestionPageState extends State<QuestionPage> {
QuizBrain quizBrain = QuizBrain(); //Change object build method to global.
@override
Widget build(BuildContext context) {
void nextQuestion() {
setState(() {
quizBrain.nextQuestion();
});
}
return SafeArea(
child: Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Center(
child: Text('Questions'),
),
Text(quizBrain.getQuestionText()),
RaisedButton(
onPressed: nextQuestion,
child: Text('Next Qns'),
),
],
),
));
}
}
答案 1 :(得分:1)
我做了一些小改动,这似乎可行:
class QuestionPage extends StatefulWidget {
static const pageId = 'QuestionPage';
@override
_QuestionPageState createState() => _QuestionPageState();
}
class _QuestionPageState extends State<QuestionPage> {
String q;
QuizBrain quizBrain;
_QuestionPageState() {
quizBrain = QuizBrain();
q = quizBrain.getQuestionText();
}
@override
Widget build(BuildContext context) {
void nextQuestion() {
setState(() {
quizBrain.nextQuestion();
q = quizBrain.getQuestionText();
});
}
return MaterialApp(
home: Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Center(
child: Text('Questions'),
),
Text(q),
RaisedButton(
onPressed: nextQuestion,
child: Text('Next Qns'),
),
],
),
),
);
}
}
QuizBrain:
class QuizBrain {
int _questionBankNumber = 0;
List<Question> _questionBank = [
Question('Why is the world round?', 'Dummy answer1', 'Dummy answer2',
'Dummy answer3', 'Dummy answer4', 'Dummy answer1'),
Question('Is the sun blue in colour?', 'Dummy answer1', 'Dummy answer2',
'Dummy answer3', 'Dummy answer4', 'Dummy answer1'),
Question('How often does the rooster moo?', 'Dummy answer1',
'Dummy answer2', 'Dummy answer3', 'Dummy answer4', 'Dummy answer1')
];
void nextQuestion() {
if (_questionBankNumber < _questionBank.length - 1) {
_questionBankNumber++;
}
}
//ASSUMPTION
String getQuestionText() {
return _questionBank[_questionBankNumber].q;
}
}
class Question {
String q;
String ans1;
String ans2;
String ans3;
String ans4;
String ans5;
Question(this.q, this.ans1, this.ans2, this.ans3, this.ans4, this.ans5);
}