Flutter 监听返回键
WillPopScope的注释:
注册一个回调函数来否决用户试图关闭封闭的[ModalRoute]。
抱歉,每个字我都认识,这一句话理解不了…
通过 WillPopScope 可以实现对返回键点击事件的监听,通过 onWillPop 回调函数可以处理响应点击事件
参考自:此文章
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: _requestPop,
child: Scaffold(
appBar: AppBar(
title: Text('工作须知'),
leading: IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {
print("退出${Navigator.canPop(context)}");
if (Navigator.canPop(context)) {
Navigator.pop(context);
} else {
SystemNavigator.pop();
}
},
),
),
),
);
}
Future<bool> _requestPop() {
print("POP");
if (Navigator.canPop(context)) {
Navigator.pop(context);
} else {
SystemNavigator.pop();
}
return Future.value(false);
}
WillPopScope 这种模式,如果不在AppBar写Ieading处理事件,是可以监听返回按钮的,这里指左上角那个返回,实体按键是检测不到的(华为P30检测不到…)
可以考虑用plugin调android 原生参考该文章
在flutter官网中Platform adaptations有回退导航介绍:
Back navigation
On Android, the OS back button, by default, is sent to Flutter and pops the top route of the WidgetsApp’s Navigator.
On iOS, an edge swipe gesture can be used to pop the top route.
不过没啥用,2个字’然并无’
本文介绍如何使用Flutter的WillPopScope组件监听返回键事件,并通过onWillPop回调函数处理响应事件。探讨了监听实体按键的局限性及解决方案,同时对比了Android和iOS平台上的回退导航行为。
3017

被折叠的 条评论
为什么被折叠?



