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
相关产品推荐
相关产品推荐

