1.物理返回退出app
定义
DateTime? currentBackPressTime;
写在Scaffold里
Scaffold(
body: WillPopScope(
onWillPop:()async {
DateTime now = DateTime.now();
if (currentBackPressTime == null ||
now.difference(currentBackPressTime!) > Duration(seconds: 2)) {
currentBackPressTime = now;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('再按一次退出应用'),
),
);
return false;
}
SystemNavigator.pop(); // 退出应用
return true;
}
))
2. 禁止物理返回
WillPopScope(
onWillPop: () async {
// 禁止返回上一页
return Future.value(false);
},
child:})
3.物理返回指定到某一页
WillPopScope(
onWillPop: () async {
Navigator.pushNamed(context, '/home');
return true;
},