Flutter - 当用户使用手机内置后退按钮时如何处理应用程序中的命令

发布于 2025-01-17 11:36:21 字数 730 浏览 2 评论 0原文

我一直在构建一个应用程序,该应用程序在应用程序栏中具有后退箭头“<-”和十字“X”。以编程方式,我包含了各种命令,包括 Navigator.pop(context) 以及清除使用 GetX 数据控制器存储的全局数据,如下所示:

        onPressed: () {
          try {
            /// Clear data, so the app is ready to use again
            c.updateValue('');

            /// Close Screen
            Navigator.pop(context);
          } catch (e) {
            print(
                'Error when closing the database input window.');
          }
        },

在这种情况下,清除值意味着只需替换任何字符串用空字符串存储。

现在,当按下应用栏中的后退箭头或十字时,此功能可以完美运行。但是,我注意到在物理设备上,当我使用手机的后退箭头/按钮(位于应用程序之外)时,当它移动屏幕时,它不会清除任何数据,根据第一个声明在我上面的 onPressed 函数中。

我的问题是,与应用程序的编程后退按钮相比,当用户使用手机的后退按钮时,如何获得相同的命令?

谢谢你!

I've been building an app that has a back arrow '<-' and cross 'X' in the app bar. Programmatically, I have included various commands, including Navigator.pop(context) and to clear global data stored with a GetX data controller, like so:

        onPressed: () {
          try {
            /// Clear data, so the app is ready to use again
            c.updateValue('');

            /// Close Screen
            Navigator.pop(context);
          } catch (e) {
            print(
                'Error when closing the database input window.');
          }
        },

In this instance, clearing the value means simply replacing whatever string is stored with an empty string.

Now, this works perfectly when either the back arrow or cross in the app bar are pressed. However, I've noticed on a physical device that when I make use of the phone's back arrow/button, which is outside of the app, whilst it moves the screen, it does not clear any of the data, as per the first statement in my onPressed function above.

My question is, how do I get the same commands to take place when the user makes use of the phone's back button, compared to the app's programmed back button?

Thank you!

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

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

发布评论

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

评论(1

凶凌 2025-01-24 11:36:21

将您的脚手架 willpopscope 包装,并在onwillpop上执行相同的命令:

return WillPopScope(
   onWillPop: () async{
      try {
        /// Clear data, so the app is ready to use again
        c.updateValue('');
        return true; // true allows navigating back
      } catch (e) {
        print(
            'Error when closing the database input window.');
        return false; // false prevents navigating back
      }
    
    },
   child: Scaffold(....),
  );

Wrap your Scaffold with WillPopScope and execute the same command on the onWillPop like this:

return WillPopScope(
   onWillPop: () async{
      try {
        /// Clear data, so the app is ready to use again
        c.updateValue('');
        return true; // true allows navigating back
      } catch (e) {
        print(
            'Error when closing the database input window.');
        return false; // false prevents navigating back
      }
    
    },
   child: Scaffold(....),
  );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文