gpt4 book ai didi

flutter - 无法更新Text Flutter Dart

转载 作者:行者123 更新时间:2023-12-03 04:26:25 29 4
gpt4 key购买 nike

我正在尝试一个问题,但由于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++;
}
}
}


最佳答案

我做了一些小的更改,这似乎正在起作用:

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

关于flutter - 无法更新Text Flutter Dart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59498914/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com