我想要更新evaluateAnswer布尔值,并在按下QuestionsList(无状态部件)按钮时用新值重新构建SubmitExamButton(有状态)部件。但它总是使用我初始化变量时使用的false值来构建它。我尝试使用SubmitExamButton创建Body有状态小部件,如下所示:
SubmitExamButton(
onPress: () {
setState(() {
evaluateAnswer = true;
});
},
),Body.dart
class Body extends StatelessWidget {
@override
Widget build(BuildContext context) {
bool evaluateAnswer = false;
return Stack(
children: [
Padding(
child: QuestionsList(
evaluate: evaluateAnswer,
),
),
Positioned(
bottom: 0,
child: Container(
height: 80,
decoration: BoxDecoration(color: Colors.white),
width: SizeConfig.screenWidth,
child: SubmitExamButton(
onPress: () {
evaluateAnswer = true;
},
),
),
),
),
],
);
}
}发布于 2020-12-25 06:17:25
您需要将状态移出到与StatefulWidget关联的evaluateAnswer类的成员变量中。然后,当您在State类的setState ()中的任何位置构建时,Stateful小部件将重新构建。将数据粘贴为无状态小部件的成员数据不会得到重新构建机制注意到的更改。
https://stackoverflow.com/questions/65443489
复制相似问题