检测并阻止用户将当前页面放在扑朔迷离中?

发布于 2025-02-07 12:27:36 字数 36 浏览 1 评论 0原文

我想检测用户何时试图离开当前页面,并可能阻止他们进行扑打。

I want to detect when a user tries to leave the current page and probably stop them from doing so in flutter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愛放△進行李 2025-02-14 12:27:37

欢迎来到堆栈溢出,
您可以使用WillPopsCope小部件来实现这一目标。
以下是实现此目标的代码示例。

WillPopScope(
          child: Container(),
          onWillPop: () {
            if (allowPop) {
              return Future.value(true);
            } else {
              return Future.value(false);
            }
          });

其中允许Pop可以是一个变量,您可以用来检查是否允许用户离开页面。

welcome to stack overflow,
You can achieve this by using the WillPopScope Widget.
Below is a code sample to achieve this.

WillPopScope(
          child: Container(),
          onWillPop: () {
            if (allowPop) {
              return Future.value(true);
            } else {
              return Future.value(false);
            }
          });

Where allowPop can be a variable you can use to check if to allow user to leave the page or not.

瞎闹 2025-02-14 12:27:37

您可以在WillPopsCope的帮助下进行此操作。

return WillPopScope(
  onWillPop: () async {
    // returning false will don't let screen pop.
    return false;
  },
  child: ... // Your Scaffold goes here.
);

You can do this with the help of WillPopScope.

return WillPopScope(
  onWillPop: () async {
    // returning false will don't let screen pop.
    return false;
  },
  child: ... // Your Scaffold goes here.
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文