我正在尝试创建一个测验应用程序,并且在我的playQuiz中,在玩家回答了正确的答案后,将生成新的答案。我试图创建一个函数“foo”并调用它,这样它就会改变按钮中的图像和文本。但是,没有任何改变,这只会在我旋转屏幕时发生变化。代码简短易懂。
class Game extends StatelessWidget {
final ForceSelection forceSelection;
Game({Key key, @required this.forceSelection}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: forceSelection.langSelection, // TODO should work and change later
theme: new ThemeData(
primaryColor: Colors.white,
),
home: PlayQuiz(),
);
}
}
class PlayQuizState extends State<PlayQuiz> {
final ForceSelection forceSelection; // TODO Does this get forceSelection
List<String> groundForce = [ 'sotamies', 'aliupseerioppilas', 'korpraali', 'alikersantti', 'upseerioppilas', 'kersantti', 'upseerikokelas', 'ylikersantti', 'vääpeli', 'ylivääpeli', 'sotilasmestari', 'vänrikki', 'luutnantti', 'yliluutnantti', 'kapteeni', 'majuri', 'everstiluutnantti', 'eversti', 'prikaatikenraali', 'kenraalimajuri', 'kenraaliluutnantti', 'kenraali'];
List<int> randomList = Helper.randomAnswers();
static var rng = new Random();
var corrAnsIndex = rng.nextInt(3);
int points = 0;
String quizImage = "";
String debugText = "";
PlayQuizState(this.forceSelection); // TODO Does this get langSelection
void _startGame() {
quizImage = 'images/' + groundForce[randomList[corrAnsIndex]].replaceAll('ä','a') + '.jpg';
}
void _nextGame() {
setState(() {
points += 1;
randomList = Helper.randomAnswers();
corrAnsIndex = rng.nextInt(3);
quizImage = 'images/' + groundForce[randomList[corrAnsIndex]].replaceAll('ä','a') + '.jpg';
debugText = forceSelection.langSelection + randomList.toString() + " " + groundForce[randomList[corrAnsIndex]].replaceAll('ä','a');
// TODO edited debug text and next is to check if langSelection is fin
});
}
void _endGame() {
setState(() {
//TODO Ask for name and send to database
});
}
@override
Widget build(BuildContext context) {
_startGame();
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.asset(quizImage),
Text("randomList: " + randomList.toString()),
Text("Oikea vastaus: " +
groundForce[randomList[corrAnsIndex]] +
" corrAnsIndex: " +
corrAnsIndex.toString()),
Text(debugText + " points: " + points.toString()),
//Text("Voima valinta: " + forceSelection = Game.forceSelection),
RaisedButton(
child: Text(groundForce[randomList[0]]),
onPressed: () {
if (0 == corrAnsIndex) {
_nextGame();
//this.foo();
}
}),
RaisedButton(
child: Text(groundForce[randomList[1]]),
onPressed: () {
if (1 == corrAnsIndex) {
_nextGame();
}
}),
RaisedButton(
child: Text(groundForce[randomList[2]]),
onPressed: () {
if (2 == corrAnsIndex) {
_nextGame();
}
}),
RaisedButton(
child: Text(groundForce[randomList[3]]),
onPressed: () {
if (3 == corrAnsIndex) {
_nextGame();
}
}),
],
)),
);
}
}
class PlayQuiz extends StatefulWidget {
@override
PlayQuizState createState() => new PlayQuizState(ForceSelection("","")); //TODO how to pass vars to ""
}
您必须使用setState重建窗口小部件才能更新视图,还要将窗口小部件更改为StatefulWidget,而不是StatelessWidget。
文件:https://docs.flutter.io/flutter/widgets/State/setState.html https://flutterdoc.com/stateful-or-stateless-widgets-42a132e529ed
我建议你应该花更多的时间来学习一些颤抖的基本想法然后继续你的发展。