Flutter UI未在setState上更新。但是在我洗牌了我想要显示的列表之后(混乱)。未解决
我正在写一个用颤振+飞镖的文字游戏 我有两行应该以字母片段的形式显示X个小部件。第一行是空的,第二行有字母,当我按下第2行的字母时,我希望第一行的片段包括这个字母 想象一个文字游戏,字母随时可以被点击以填充空框来创建正确的单词 我有三个班,主要班,字母班,空白班。 包括一些变量,如字母。EmptyLiterPiece具有默认字母=“”;并将按下按钮设置为字母 Main有一个statefulwidget,其他的是无状态widget 我为LetterPiece和EmptyLetterPiece对象创建新列表。 长度取决于我选择的单词 我的问题是: “A”是我在按下字母时指定的测试值,它们在我打印出来时得到值,但UI没有更新。除非我->emptyPieces.shuffle();每次在generate方法中 然后,emptyPieces中的对象显示“A” 我不想每次设置state时都洗牌,但这会使UI更新,而如果我不洗牌,UI将不会更新 继续读下去 我的主类有一个Statefulwidget来更新ui 我还尝试向其他类添加Statefulwidget,但没有成功Flutter UI未在setState上更新。但是在我洗牌了我想要显示的列表之后(混乱)。未解决,flutter,dart,setstate,Flutter,Dart,Setstate,我正在写一个用颤振+飞镖的文字游戏 我有两行应该以字母片段的形式显示X个小部件。第一行是空的,第二行有字母,当我按下第2行的字母时,我希望第一行的片段包括这个字母 想象一个文字游戏,字母随时可以被点击以填充空框来创建正确的单词 我有三个班,主要班,字母班,空白班。 包括一些变量,如字母。EmptyLiterPiece具有默认字母=“”;并将按下按钮设置为字母 Main有一个statefulwidget,其他的是无状态widget 我为LetterPiece和EmptyLetterPiece对象创
//MY BUILD METHOD
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomLeft,
stops: [0.3, 1],
colors: [
Color(firstColor),
Color(secondColor)
])
),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Text(word1, textAlign: TextAlign.center, style: TextStyle(fontSize: textScale, color: Colors.white),),
generateEmptyPiecesRow(), // <---
generateLetterPiecesRow() // <---
],
),
);
}
//THE TWO GENERATE METHODS.
Widget generateEmptyPiecesRow(){
if(emptyPieces.length < word2.length) {
for (int i = 0; i < word2.length; i++) {
emptyPieces.add(new EmptyLetterPiece(pieceTextScale, 5, 50));
print("Added. $i");
}
emptyPieces.shuffle();
}
//emptyPieces.shuffle(); <--- WORKS IF I PUT THIS HERE, UI UPDATES ONLY IF I SHUFFLE THE PIECES BEFORE RETURNING THEM WITH THE ROW. <---
return Row(mainAxisAlignment: MainAxisAlignment.center, children: emptyPieces);
}
Widget generateLetterPiecesRow(){
if(letterPieces.length < word2.length) {
for (int i = 0; i < word2.length; i++) {
letterPieces.add(new LetterPiece(
word2.substring(i, i + 1).toUpperCase(), pieceTextScale, 15,
addLetter, removeLetter));
}
letterPieces.shuffle();
}
return Row(mainAxisAlignment: MainAxisAlignment.center, children: letterPieces);
}
//setState methods
addLetter(){
setState(() {
emptyPieces[getPiecePos()].setLetter("A");
increasePiecePos();
});
}
removeLetter(){
setState(() {
emptyPieces[getPiecePos()].removeLetter();
decreasePiecePos();
});
}
我看了一下这篇文章->当您在想要刷新的小部件中调用SetState时,视图将被更新。子窗口小部件也将被刷新。如果您不想刷新小部件本身或其子小部件,有其他方法可以实现这一点 谢谢你提供的信息。但我不确定您是否提供了解决方案。我的问题是,只有在返回生成方法中使用的行之前洗牌列表,UI才会更新。我看不出洗牌列表与更新UI之间的相关性。否则UI也应该更新。这让我有点困惑。把你的问题分解成一小段可以快速复制、粘贴和编译的代码会很有帮助。打开我的帖子
// EDIT: THIS IS THE EmptyLetterPiece class build method, LetterPiece has the same structure too.
@override
Widget build(BuildContext context) {
// TODO: implement build
return new Container(
margin: EdgeInsets.all(_padding),
width: _pieceScale,
height: _pieceScale,
decoration: BoxDecoration(
border: Border.all(color: Colors.white, width: 3),
borderRadius: new BorderRadius.all(Radius.circular(20.0)),
),
child: Center(
child: Text(_letter, textAlign: TextAlign.center, style: TextStyle(color: Colors.white), textScaleFactor: _textScale),
)
);
}