Dart 当双击/单击Flatter中的onBack按钮时,我需要关闭我的应用程序
我是个新手,我看到很多android应用程序在双击后退按钮时都可以退出 第一次按“后退”按钮时,应用程序将显示一个祝酒词“再次按可退出应用程序”。第二次按下后,应用程序退出。当然,两次按压之间的时间不能太长 我使用了这个代码,但它不起作用。请帮我解决这个问题Dart 当双击/单击Flatter中的onBack按钮时,我需要关闭我的应用程序,dart,flutter,flutter-layout,Dart,Flutter,Flutter Layout,我是个新手,我看到很多android应用程序在双击后退按钮时都可以退出 第一次按“后退”按钮时,应用程序将显示一个祝酒词“再次按可退出应用程序”。第二次按下后,应用程序退出。当然,两次按压之间的时间不能太长 我使用了这个代码,但它不起作用。请帮我解决这个问题 Future<bool> _onWillPop() { return showDialog( context: ctx, builder: (ctx) => new AlertDialog(
Future<bool> _onWillPop() {
return showDialog(
context: ctx,
builder: (ctx) => new AlertDialog(
title: new Text('Confirm Exit?',
style: new TextStyle(color: Colors.black, fontSize: 20.0)),
content: new Text(
'Are you sure you want to exit the app? Tap \'Yes\' to exit \'No\' to cancel.'),
actions: <Widget>[
new FlatButton(
onPressed: () {
// this line exits the app.
SystemChannels.platform
.invokeMethod('SystemNavigator.pop');
},
child:
new Text('Yes', style: new TextStyle(fontSize: 18.0)),
),
new FlatButton(
onPressed: () => Navigator.pop(ctx), // this line dismisses the dialog
child: new Text('No', style: new TextStyle(fontSize: 18.0)),
)
],
),
) ??
true;
}
Future\u onWillPop(){
返回显示对话框(
背景:ctx,
生成器:(ctx)=>新建警报对话框(
标题:新文本(“确认退出”,
样式:新文本样式(颜色:Colors.black,fontSize:20.0)),
内容:新文本(
“是否确实要退出应用程序?点击“是”退出“否”取消”。),
行动:[
新扁平按钮(
已按下:(){
//该行退出应用程序。
SystemChannels.platform
.invokeMethod('SystemNavigator.pop');
},
儿童:
新文本(“是”,样式:新文本样式(fontSize:18.0)),
),
新扁平按钮(
onPressed:()=>Navigator.pop(ctx),//此行将取消对话框
子项:新文本(“否”,样式:新文本样式(fontSize:18.0)),
)
],
),
) ??
是的;
}
这是一个示例代码:它使用“颤栗土司”来显示土司
DateTime currentBackPressTime = DateTime.now();
Future<bool> onWillPop() {
DateTime now = DateTime.now();
if (now.difference(currentBackPressTime) > Duration(seconds: 2)) {
currentBackPressTime = now;
Fluttertoast.showToast(msg: 'Tap Again to Exit'); // you can use snackbar too here
return Future.value(false);
}
return Future.value(true);
}
DateTime currentBackPressTime=DateTime.now();
未来的onWillPop(){
DateTime now=DateTime.now();
如果(现在.差异(currentBackPressTime)>持续时间(秒:2)){
currentBackPressTime=现在;
flattertoast.showtoos(消息:“再次点击退出”);//这里也可以使用snackbar
返回Future.value(false);
}
返回未来值(true);
}
它的工作非常完美。
Future<bool> _onWillPop() {
return showDialog(
barrierDismissible: false,
context: ctx,
builder: (context) => new AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12.0)),
content: new Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Icon(
Icons.exit_to_app,
color: MyColor.PrimaryColor,
size: 25.0,
),
SizedBox(width: 8.0),
Expanded(
child: Text(
"Do you want to exit this App",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 17.0,
color: Colors.black),
),
),
],
),
actions: <Widget>[
new FlatButton(
onPressed: ()
{
Navigator.of(context).pop(false); },
child: new Text('No',style: TextStyle(color: MyColor.PrimaryColor,fontSize: 17.0)),
),
new FlatButton(
onPressed: () {
Navigator.pop(context,true);
Navigator.of(context).pop(true);},
child: new Text('Yes',style: TextStyle(color:MyColor.PrimaryColor,fontSize: 17.0)),
),
],
),
) ?? false;
}
Future\u onWillPop(){
返回显示对话框(
禁止:错误,
背景:ctx,
生成器:(上下文)=>新建警报对话框(
形状:圆形矩形边框(
边界半径:边界半径。圆形(12.0)),
内容:新行(
mainAxisAlignment:mainAxisAlignment.start,
儿童:[
图标(
Icons.exit_to_应用程序,
颜色:MyColor.PrimaryColor,
尺寸:25.0,
),
尺寸箱(宽度:8.0),
扩大(
子:文本(
“是否要退出此应用”,
样式:TextStyle(
fontWeight:fontWeight.bold,
字体大小:17.0,
颜色:颜色。黑色),
),
),
],
),
行动:[
新扁平按钮(
已按下:()
{
Navigator.of(context.pop(false);},
子项:新文本(“否”,样式:TextStyle(颜色:MyColor.PrimaryColor,fontSize:17.0)),
),
新扁平按钮(
已按下:(){
pop(上下文,true);
Navigator.of(context.pop(true);},
子项:新文本(“是”,样式:TextStyle(颜色:MyColor.PrimaryColor,fontSize:17.0)),
),
],
),
)??假;
}
尝试使用退出(0)代码>