[英]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.