注意:onWillPop的返回值是一个Future的值。 返回 Future.value(false); 表示不退出。返回 Future.value(true); 表示退出.
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return new WillPopScope(
onWillPop: onBackPressed,
child: Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Text(
'学习WillPopScope的使用',
),
),
),
);
}
Future<bool> onBackPressed() {
return showDialog(
context: context,
builder: (BuildContext context) {
return new AlertDialog(
title: new Text("是否退出应用程序"),
actions: <Widget>[
new FlatButton(
onPressed: () {
Navigator.of(context).pop(false);
},
child: Text("确认")),
new FlatButton(
onPressed: () {
Navigator.of(context).pop(true);
},
child: Text("取消")),
],
);
});
}
/**
* 另外一种场景:Android端返回按钮点击两次,就直接退出应用
* 需要一个变量保存两次点击之间的时间间隔,根据这个时间间隔返回true或false
*/
int lastClickTime = 0;
Future<bool> doubleClickToBack() {
int now = DateTime.now().millisecond;
if ((now - lastClickTime) > 1000) {
lastClickTime = DateTime.now().millisecond;
return Future.value(false);
} else {
return Future.value(true);
}
}