如何让 Flutter 中的 Alert Dialog 在 30 秒后消失?

How to make Alert Dialog disappear after 30 seconds in Flutter?

所以我在我的项目中使用了一个警告对话框,如果用户单击 'Ok' 或 'Cancel',它应该关闭,否则如果用户没有给出,它应该在 30 秒后自行消失回复。我正在考虑使用 setStateDuration 或类似的东西,但无法完全弄清楚。

您可以使用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
});