我正在尝试创建测验应用程序,并且在我的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 ""
}
答案 0 :(得分:2)
您必须使用setState来重建窗口小部件以更新视图,还必须将窗口小部件更改为StatefulWidget,而不是StatelessWidget。
文档:https://docs.flutter.io/flutter/widgets/State/setState.html https://flutterdoc.com/stateful-or-stateless-widgets-42a132e529ed
我建议您应该花更多的时间来学习颤振的一些基本概念,然后继续发展。