[英]How to manage the state in Get.bottomSheet? flutter
当我使用 Get.bottomSheet 时,即使我用 GetBuilder() 包装它并使用 update() 函数,我也无法管理状态。
这是代码
这是在底页中显示评论的功能。
commentFunction(context, id, comments) {
Get.bottomSheet(CustomPageForComments(
postid: id,
sendCommentFunction: () {
sendComment(id, commentController.text);
},
controllerForCommentField: commentController,
comments: comments));}
CustomPageForComments() 是一个简单的页面,它使用流来从数据库中获取评论。
最后这是发送评论功能
sendComment(String id, String message) {
commentController.clear();
DocumentReference setter =
FirebaseFirestore.instance.collection("Posts").doc(id);
setter.update({
"comments": FieldValue.arrayUnion([
{
"username": currentusername,
"message": message,
"time": DateTime.now(),
"id": formatDate(DateTime.now(),
[yyyy, ":", mm, ":", dd, ":", hh, ':', nn, ":", ss]),
}
])
});
update();}
我想在使用发送评论功能时刷新底页,这样用户就可以在不重新打开底页的情况下看到他的评论。
当您关闭底部工作表并重新打开它时,它实际上会更新
我该如何管理?
我还想问一下,在评论中使用 Stream Builder 而不是 Future Builder 是真的吗?
谢谢你
要更改底部工作表内的状态,您可以使用StatefulBuilder
Get.bottomSheet(
StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return Container(
height: heightOfModalBottomSheet,
child: RaisedButton(onPressed: () {
setState(() {
// Change state here
});
}),
);
});
)
如果您希望评论实时更新(由于读取可能会产生更高的成本),请使用Stream Builder ,使用FutureBuilder获取一次数据
在 bottomSheet 中使用 update() 一切正常。 确保正确使用 GetBuilder。 您需要刷新内部小部件,而不是 Get.bottomSheet 函数。 您的代码应如下所示:
Get.bottomSheet(
GetBuilder<CommentController>(builder: (cLogic) {
return Text('Comments count: ${cLogic.count}');
})
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.