简体   繁体   English

如何在 AlertDialog 关闭时刷新 Flutter UI?

[英]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:到目前为止我尝试过的内容如下:

  1. I update the data context (the passed values) before I navigate to the previous screen我在导航到上一个屏幕之前更新数据上下文(传递的值)
  2. I use StatefulWidget in order to add interactivity to my app我使用 StatefulWidget 为我的应用程序添加交互性
  3. I use setState to change the data before I return to the previous screen but without any success.在返回上一个屏幕之前,我使用 setState 更改数据,但没有成功。
  4. I found multiple ways on google how to "refresh" UI but without any success我在谷歌上找到了多种方法来“刷新”用户界面,但没有任何成功
  5. I try to use setState() since it's perfectly working with some data and I got the following error: " A build function returned null."我尝试使用 setState(),因为它可以完美地处理一些数据,但出现以下错误:“构建函数返回空值。”
  6. I add setState as part of ShowDialog and I got the following error: "setState() or markNeedsBuild() called during the build."我将 setState 添加为 ShowDialog 的一部分,但出现以下错误:“在构建期间调用了 setState() 或 markNeedsBuild()。” so I add additional logic to check if the data is dirty or not but at the end of a day I got the same result.所以我添加了额外的逻辑来检查数据是否脏,但在一天结束时我得到了相同的结果。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM