Flutter - 当用户使用手机内置后退按钮时如何处理应用程序中的命令
我一直在构建一个应用程序,该应用程序在应用程序栏中具有后退箭头“<-”和十字“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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将您的脚手架用 willpopscope 包装,并在
onwillpop
上执行相同的命令:Wrap your Scaffold with WillPopScope and execute the same command on the
onWillPop
like this: