如何关闭Flutter中的所有底部模态表单。

huangapple go评论56阅读模式
英文:

How to close all the Modal bottom sheet in flutter

问题

我正在创建我的应用中一系列的底部模态弹出窗口。当我从模态底部窗口 A 转到模态底部窗口 B 时,应关闭模态底部窗口 A,我不知道如何实现这一点,当我使用 navigator.pop 时,它导航回了底部窗口 A。

英文:

I'm creating series of modal bottom sheet in my app. When I go to A modal bottom sheet to B modal bottom sheet then A modal bottom sheet should be closed, I'm not getting how to achieve this, when I use navigator.pop, then it's navigating to A bottom sheet.

答案1

得分: 1

在进入 B 底部表之前,应调用 Navigator.pop

示例:

InkWell(
    onTap: () {
        showModalBottomSheet(
            context: context,
            builder: (context) {
                return InkWell(
                    onTap: () {
                        Navigator.of(context).pop();
                        showModalBottomSheet(
                            context: context,
                            builder: (context) {
                                return const Text("Bottom Sheet B");
                            });
                    },
                    child: const Text("Bottom Sheet A"));
            });
    },
    child: const Text("Home"),
),
英文:

Before you go to B bottom sheet, you should call Navigator.pop

Example:

InkWell(
            onTap: () {
              showModalBottomSheet(
                  context: context,
                  builder: (context) {
                    return InkWell(
                        onTap: () {
                          Navigator.of(context).pop();
                          showModalBottomSheet(
                              context: context,
                              builder: (context) {
                                return const Text("Bottom Sheet B");
                              });
                        },
                        child: const Text("Bottom Sheet A"));
                  });
            },
            child: const Text("Home"),
          ),

答案2

得分: 0

你可以在关闭时从表格 A 返回状态。根据该状态打开表格 B。

示例:

InkWell(
  child: Text('Press me'),
  onTap: () async {
    final result = await showModalBottomSheet(
      context: context,
      builder: (context) => InkWell(
        child: Text('打开表格 B'),
        onTap: () => Navigator.pop(context, true),
      ),
    );
    print(result);
    if (mounted && result == true) {
      showModalBottomSheet(
        context: context,
        builder: (context) => InkWell(
          child: Text('关闭表格 B'),
          onTap: () => Navigator.pop(context, true),
        ),
      );
    }
  },
)
英文:

You can simply return the status from sheet A while closing. Based on that open sheet B.

Example:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

InkWell(
                      child: Text(&#39;Press me&#39;),
                      onTap: () async {
                        final result = await showModalBottomSheet(
                          context: context,
                          builder: (context) =&gt; InkWell(
                            child: Text(&#39;Open SHeet B&#39;),
                            onTap: () =&gt; Navigator.pop(context, true),
                          ),
                        );
                        print(result);
                        if (mounted &amp;&amp; result == true) {
                          showModalBottomSheet(
                            context: context,
                            builder: (context) =&gt; InkWell(
                              child: Text(&#39;CLose SHeet B&#39;),
                              onTap: () =&gt; Navigator.pop(context, true),
                            ),
                          );
                        }
                      },
                    ),

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月29日 14:04:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76354997.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定