如何在Flutter中解除showDialog之后的运行代码?

时间:2018-04-07 09:50:34

标签: dart flutter

如何在showDialog()被解雇/处置后更新主页?似乎它没有onDispose()函数。

找到另一个可能的答案:'WillPopScope'可以帮助检测后退按钮是否被按下。

将在'showDialog'中使用的小部件,在其构建函数中,小部件可以包含在'return new WillPopScope(child:______,onWillPop:_______)中;代码可以在'onWillPop'函数中运行。这可以更新下面的主页。

7 个答案:

答案 0 :(得分:12)

只需使用await,然后在关闭对话框后运行“ then”块中的代码

await showDialog(
       //Your Dialog Code
).then((val){
    Navigator.pop(_context);
});

答案 1 :(得分:8)

这实际上取决于您想要的更新类型。

但是,这是一个简单的例子,可以帮助你弄明白。

enter image description here

using System.Web.Http;

答案 2 :(得分:4)

移动应用程序通常通过名为"屏幕"的全屏元素显示其内容。或"页面"。在Flutter中,这些元素称为路径,它们由导航器小部件管理。导航器管理一堆Route对象,并提供管理堆栈的方法,如Navigator.pushNavigator.pop

showDialog(
            context: context,
            child: new AlertDialog(
                title: const Text("Your Title"),
                content: const Text(
                  ...
                   Your Message
                  ...),
                actions: [
                new FlatButton(
                  child: const Text("Ok"),
                  onPressed: () => Navigator.pop(context),
                ),
              ],
            ),
        );

您可以查看Official Document

答案 3 :(得分:2)

如果您为对话框创建了另一个StatefulWidget且onDissmissed,则要在“对话框调用窗口小部件”中调用某些函数。您可以使用此代码段。

await showDialog(
      context: context,
      builder: (_) => MenuBox(),
);
print("now closed");

答案 4 :(得分:1)

使用; .whenComplete(funcName/*not funcName() !*/);showDialog()

之后
funcName() {
//Your code
}

答案 5 :(得分:0)

由于上面不建议使用AlertDialog对象使用的“子级”构造,上面提到的几个答案有些过时了。

这是我现在用来代替警报提示对话框的

          showDialog(
            context: context,
            builder: (BuildContext context) {
              // return object of type Dialog
              return AlertDialog(
                title: new Text("Conversation Request"),
                content:
                    new Text("Have a conversation with this person"),
                actions: <Widget>[
                  // usually buttons at the bottom of the dialog
                  new FlatButton(
                    child: new Text("Accept"),
                    onPressed: () {
                      performAccept();
                    },
                  ),
                  new FlatButton(
                    child: new Text("Ignore"),
                    onPressed: () {
                      performIgnore();
                    },
                  )
                ],
              );
            },
          )

答案 6 :(得分:0)

有两种方法。

  1. 使用async-await

    bool shouldUpdate = await showDialog<bool>(...);
    if (shouldUpdate) {
      setState(() {
        // we should update the UI
      });
    }
    
  2. 使用then

    showDialog<bool>(...).then((shouldUpdate) {
      if (shouldUpdate) {
        setState(() {
          // we should update the UI
        });
      }
    });