[英]How to refresh flutter UI on AlertDialog close?
I'm working on some sort of time tracking application.我正在研究某种时间跟踪应用程序。 The main idea behind it is simple - add the number of hours for a wanted project and display the sum of hours per day.
它背后的主要思想很简单——为一个想要的项目添加小时数并显示每天的小时数总和。
I'm using Flutter and dart for this and I have the following: - List of cards for every weekday - Cards have Gesture detector tap implemented - this action will show all projects for the selected day - AlertDialog pop-up to add the hours我为此使用 Flutter 和 dart,我有以下内容: - 每个工作日的卡片列表 - 卡片具有手势检测器点击 - 此操作将显示所选日期的所有项目 - AlertDialog 弹出窗口以添加小时数
I'm trying to refresh the total number of hours for the selected day on Alert Dialog pop-up close without reloading all the data.我正在尝试在警报对话框弹出关闭时刷新所选日期的总小时数,而无需重新加载所有数据。
Can someone help me with any idea how to implement the refresh?有人可以帮助我了解如何实现刷新吗?
What I've tried so far is the following:到目前为止我尝试过的内容如下:
Desired behavior for this will be to click on the card and display all the projects for the selected day.为此所需的行为是单击卡片并显示所选日期的所有项目。 After adding a new value I want to return to the same place (with opened card) and the newly added project will show up on the list along with updated total hours for the day.
添加新值后,我想返回到同一个地方(使用打开的卡),新添加的项目将显示在列表中,以及当天更新的总小时数。
For now, I got edit partially done.现在,我已经部分完成了编辑。 I open the card and edit one of the existing project.
我打开卡片并编辑现有项目之一。 The data is saved, but when I return to the previous screen I cannot update the hours on the UI.
数据已保存,但当我返回上一个屏幕时,我无法更新 UI 上的小时数。 When I open the edit screen once more the shown data is as expected.
当我再次打开编辑屏幕时,显示的数据符合预期。
First you need to call your showDialog
within a Stateful Widget
in order to have access, has the name suggests, to a state.首先,您需要在
Stateful Widget
中调用您的showDialog
以便访问(顾名思义)状态。 Then, within that screen, you can call a method and display your dialog and it will return a Future<void>
which means you can perform so action after the dialog is dismissed, such as然后,在该屏幕中,您可以调用一个方法并显示您的对话框,它将返回一个
Future<void>
这意味着您可以在对话框关闭后执行 so 操作,例如
showDialog(// Yours params).then((_)=>setState((){}));
and your UI will be updated to match changes.并且您的 UI 将更新以匹配更改。
Return true in pop Navigator from the dialog when want to refresh the screen当想要刷新屏幕时,在弹出的导航器中从对话框中返回 true
FlatButton(
onPressed: () {
Navigator.of(context).pop(true);
},
child: Text("Close"))
Refresh screen when getting true in result结果为真时刷新屏幕
final result = await showDialog<bool>(
context: context,
builder: (context) => MyDialog(),
)
if(result){// refresh screen}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.