【问题标题】:How to hide alertDialog after one second with Navigator.of(context).pop()如何在一秒钟后使用 Navigator.of(context).pop() 隐藏 alertDialog
【发布时间】:2019-08-07 13:11:56
【问题描述】:

我正在尝试在一秒钟后自动隐藏 alertDialog

这是代码:

Widget popupWidget(BuildContext context, ...) {
  Future.delayed(Duration(seconds: 1), () {
    Navigator.of(context).pop();
  });
  return AlertDialog(...);}

什么有效:

  • 我可以单击屏幕上的其他位置来关闭 alertDialog

  • 我可以等待一秒钟,它会自动关闭

错误:

如果在(确切地?)一秒后,我点击屏幕上的其他位置(关闭 alertDialog),Future.delayed(...) 不会隐藏 alertDialog,而是整个屏幕

我尝试使showDialog异步失败,也尝试了该行

Navigator.of(context, rootNavigator: true).pop();

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    我想我找到了解决方法:

    bool popupIsActive = true;
    
    Future.delayed(Duration(seconds: 1), () {
    if (popupIsActive) Navigator.of(context).pop();
    });
    

    showDialog(...).then((_) {
    popupIsActive = false;
    });
    

    【讨论】:

      【解决方案2】:

      您可能希望忽略对话框外的任何点击,以防止它被关闭并等待未来完成。您可以在showDialog() 中将barrierDismissible 设置为false。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-28
        • 2021-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-21
        相关资源
        最近更新 更多