You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中打开新弹窗时如何自动关闭原弹窗?

解决方案

要实现打开_emailSentDialog时自动关闭EmailVerifyDialog,只需在展示第二个弹窗之前,调用Navigator.pop(context)关闭当前的第一个弹窗即可。

关键修改步骤

修改Send Again按钮的点击事件逻辑,先关闭当前弹窗,再打开新弹窗:

onPressed: () async {
  await cubit.sendConfirmationCode(email: cubit.currentEmail).then((value) {
    if (value) {
      // 关闭当前的EmailVerifyDialog
      Navigator.pop(context);
      // 打开_emailSentDialog
      _emailSentDialog(context);
    } else {
      _errorNotification(context);
    }
  });
  log(cubit.currentEmail);
},

说明

  • Navigator.pop(context)会将当前的EmailVerifyDialog从导航栈中移除,这里的context指向的就是EmailVerifyDialog的上下文,对应弹窗所在的导航层级。
  • 必须先执行pop再调用showDialog,确保第一个弹窗完全关闭后再弹出第二个,避免出现弹窗叠加的问题。

修改后的完整关键代码片段

TextButton(
  style: TextButton.styleFrom(padding: EdgeInsets.zero),
  onPressed: () async {
    await cubit.sendConfirmationCode(email: cubit.currentEmail).then((value) {
      if (value) {
        Navigator.pop(context);
        _emailSentDialog(context);
      } else {
        _errorNotification(context);
      }
    });
    log(cubit.currentEmail);
  },
  child: const Text(
    'Send Again',
    style: constants.Styles.smallBookUnderlineTextStyleWhite,
  ),
),

内容的提问来源于stack exchange,提问作者Max

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 22:54:18