new MaterialButton(
onPressed: ()
{
yellowSelect();
},
child: new Text(game.yellowArmy.toString(), style: TextStyle(fontSize: 30)),
shape: Border.all(color: game.isYellow ? Colors.white : Colors.black, width: 5),
color: Colors.yellow,
height: 150,
minWidth: 150,
),
//Function called by the material button
void yellowSelect() {
setState(() {
if (game.isYellow){
game.isYellow = false;
debugPrint(game.isYellow.toString());
}
else if (!game.isYellow)
{
game.isYellow = true;
}
if (game.isYellow && game.isRed)
{
attack(game, "Yellow", "Red");
}
if (game.isYellow && game.isBlue)
{
attack(game, "Yellow", "Blue");
}
if (game.isYellow && game.isGreen)
{
attack(game, "Yellow", "Green");
}
for (NeutralArmy n in game.returnArr())
{
if (game.isYellow && n.isGray)
{
attack(game, "Yellow", "Gray", n);
}
}
});
}
这是我尝试用来单击时更新按钮之一的代码的一部分。它曾经工作得非常好,但是突然间按钮在单击时什么也没做。如果需要,我可以发布其余代码。
编辑:https://github.com/SVashi/game_basis是我完整的代码所在,谢谢。
答案 0 :(得分:0)
在调用yellowSelect()的上使用SchedulerBinding setState,
SchedulerBinding.instance
.addPostFrameCallback((_) =>
setState(() {
if (game.isYellow) {
game.isYellow = false;
debugPrint(game.isYellow.toString());
}
else if (!game.isYellow) {
game.isYellow = true;
}
if (game.isYellow && game.isRed) {
attack(game, "Yellow", "Red");
}
if (game.isYellow && game.isBlue) {
attack(game, "Yellow", "Blue");
}
if (game.isYellow && game.isGreen) {
attack(game, "Yellow", "Green");
}
for (NeutralArmy n in game.returnArr()) {
if (game.isYellow && n.isGray) {
attack(game, "Yellow", "Gray", n);
}
}
}));