Willposcope无法检测到Android后退按钮已按下 前提・我想要达到的目标
我计划在按下Android后退按钮时使用Willposcope禁用它 问题・错误消息 将脚手架封闭在Willposcope中并按下Android后退按钮不会执行Willposcope中设置的功能 相应的源代码Willposcope无法检测到Android后退按钮已按下 前提・我想要达到的目标,android,flutter,dart,Android,Flutter,Dart,我计划在按下Android后退按钮时使用Willposcope禁用它 问题・错误消息 将脚手架封闭在Willposcope中并按下Android后退按钮不会执行Willposcope中设置的功能 相应的源代码 Future onWillPopScope()异步{ 打印('on'); 返回false; } 小部件构建(构建上下文){ 返回ChangeNotifierProvider( 创建:()=>xxxModel(), 子:堆栈( 孩子们[ 威尔波普示波器( onWillPop:onWillPo
Future onWillPopScope()异步{
打印('on');
返回false;
}
小部件构建(构建上下文){
返回ChangeNotifierProvider(
创建:()=>xxxModel(),
子:堆栈(
孩子们[
威尔波普示波器(
onWillPop:onWillPopScope,
孩子:脚手架(
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
)
),
消费者(
~~~~~~~~~~~~~~~~~~~~
),
],
),
);
}
我试过的
我改变了Willposcope的位置,但它不起作用
补充信息(FW/工具版本等)
・颤振1.17.5
・Dart 2.8.4如果您使用的是导航应用程序,请尝试以下步骤:
Future<bool> _onBackPressed() {
return showDialog(
context: context,
builder: (context) => new AlertDialog(
title: new Text('Are you sure?'),
content: new Text('Do you want to exit an App'),
actions: <Widget>[
new GestureDetector(
onTap: () => Navigator.of(context).pop(false),
child: Text("NO"),
),
SizedBox(height: 16),
new GestureDetector(
onTap: () {
exit(0);
},
child: Text("YES"),
),
],
),
) ??
false;
}
Widget build(BuildContext context) {
return Scaffold(
key: _globalKey,
body: WillPopScope(
child: _getBody(_currentIndex),//your widget list index wise
onWillPop: () async {
if (_globalKey.currentState.isDrawerOpen) {
Navigator.pop(context); // closes the drawer if opened
return Future.value(false); // won't exit the app
} else {
//only back to o index
if (_currentIndex == 0) _onBackPressed();
//return true;
setState(() {
_currentIndex = 0;
});
return false;
_onBackPressed();
// return Future.value(true); // exits the app
}
},
),
);
}
Future\u onBackPressed(){
返回显示对话框(
上下文:上下文,
生成器:(上下文)=>新建警报对话框(
标题:新文本(“你确定吗?”),
内容:新文本(“是否要退出应用程序”),
行动:[
新手势检测器(
onTap:()=>Navigator.of(context.pop)(false),
子项:文本(“否”),
),
尺寸箱(高度:16),
新手势检测器(
onTap:(){
出口(0);
},
子项:文本(“是”),
),
],
),
) ??
虚假的;
}
小部件构建(构建上下文){
返回脚手架(
键:_globalKey,
正文:Willposcope(
子项:_getBody(_currentIndex),//小部件列表索引
onWillPop:()异步{
if(_globalKey.currentState.isDrawerOpen){
Navigator.pop(context);//打开抽屉时关闭抽屉
返回Future.value(false);//将不退出应用程序
}否则{
//仅返回o索引
如果(_currentIndex==0)_onBackPressed();
//返回true;
设置状态(){
_currentIndex=0;
});
返回false;
_onBackPressed();
//返回Future.value(true);//退出应用程序
}
},
),
);
}
Future<bool> _onBackPressed() {
return showDialog(
context: context,
builder: (context) => new AlertDialog(
title: new Text('Are you sure?'),
content: new Text('Do you want to exit an App'),
actions: <Widget>[
new GestureDetector(
onTap: () => Navigator.of(context).pop(false),
child: Text("NO"),
),
SizedBox(height: 16),
new GestureDetector(
onTap: () {
exit(0);
},
child: Text("YES"),
),
],
),
) ??
false;
}
Widget build(BuildContext context) {
return Scaffold(
key: _globalKey,
body: WillPopScope(
child: _getBody(_currentIndex),//your widget list index wise
onWillPop: () async {
if (_globalKey.currentState.isDrawerOpen) {
Navigator.pop(context); // closes the drawer if opened
return Future.value(false); // won't exit the app
} else {
//only back to o index
if (_currentIndex == 0) _onBackPressed();
//return true;
setState(() {
_currentIndex = 0;
});
return false;
_onBackPressed();
// return Future.value(true); // exits the app
}
},
),
);
}