【问题标题】:How to make Alert Dialog disappear after 30 seconds in Flutter?如何在 Flutter 中让警报对话框在 30 秒后消失?
【发布时间】:2020-03-19 13:55:28
【问题描述】:

所以我在我的项目中使用了一个警报对话框,如果用户单击“确定”或“取消”,它应该关闭,否则如果用户没有响应,它应该在 30 秒后自行消失。我正在考虑使用 setStateDuration 或类似的东西,但无法完全弄清楚。

【问题讨论】:

    标签: flutter dart flutter-animation


    【解决方案1】:

    您可以使用Future.delayed 进行延迟操作:

    // dialog builder
    showDialog(context, builder: (BuildContext context) {
      bool manuallyClosed = false;
      Future.delayed(Duration(seconds: 30)).then((_) {
        if (!manuallyClosed) {
          Navigator.of(context).pop());
        }
      });
    
      // Build the dialog window
      // Set manuallyClosed to true on the OK or Cancel button tap
    });
    

    【讨论】:

    • 稍作修正 .then() 不能采用空参数函数,所以我使用 then((_){})
    • @HarshilChaudhary 从记忆中编码很有趣。
    猜你喜欢
    • 2021-11-10
    • 2019-01-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2019-05-06
    • 2020-10-27
    相关资源
    最近更新 更多