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

Flutter AlertDialog切换页面后仍未关闭的问题求助

解决方法

核心问题是你在选择图片/拍完照后,没有主动关闭弹出的AlertDialog,导致对话框会跟着页面跳转继续显示。只需要在获取到媒体文件后先关闭对话框,再执行页面跳转即可。

修改后的代码如下:

void openGallery() {
  showDialog(
    context: context,
    builder: (ctx) => AlertDialog(
      contentPadding: const EdgeInsets.all(10.0),
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.zero,
      ),
      content: SizedBox(
        height: 260.0,
        width: 360.0,
        child: Column(
          children: [
            const SizedBox(height: 20.0),
            ListTile(
              onTap: () async {
                final image = await getMedia(ImageSource.gallery);
                // 关闭当前的AlertDialog
                Navigator.of(ctx).pop();
                
                if (image != null) {
                  setState(() {
                    files = image;
                  });
                  // 跳转到添加标题页面,替换成你实际的页面组件
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => const AddTitlePage()),
                  );
                }
              },
              title: Text(
                '从图库选择照片',
                style: GoogleFonts.mada(
                  fontSize: 18.0,
                  fontWeight: FontWeight.w300,
                  letterSpacing: 1.1,
                ),
              ),
              trailing: const IconButton(
                onPressed: null,
                icon: Icon(MdiIcons.memory),
              ),
            ),
            ListTile(
              onTap: () async {
                final image = await getMedia(ImageSource.camera);
                // 关闭当前的AlertDialog
                Navigator.of(ctx).pop();
                
                if (image != null) {
                  setState(() {
                    files = image;
                  });
                  // 跳转到添加标题页面,替换成你实际的页面组件
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => const AddTitlePage()),
                  );
                }
              },
              title: Text(
                '拍摄照片',
                style: GoogleFonts.mada(
                  fontSize: 18.0,
                  fontWeight: FontWeight.w300,
                  letterSpacing: 1.1,
                ),
              ),
              trailing: const IconButton(
                onPressed: null,
                icon: Icon(Icons.camera),
              ),
            ),
          ],
        ),
      ),
    ),
  );
}

几个关键改动:

  • 每次获取媒体文件后,调用Navigator.of(ctx).pop()关闭对话框,这里要用对话框的上下文ctx,确保只关闭当前弹出的对话框
  • 增加image != null判断,避免用户取消选择后执行后续逻辑
  • 添加页面跳转代码,记得把AddTitlePage换成你项目里实际的添加标题页面

这样调整后,用户选完图片后对话框会立刻关闭,然后跳转到添加标题页面,不会出现对话框跟随跳转的问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 11:36:59