通过WillPopScope
处理应用
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: _onBackPressed,
child: _child(),
);
}
提示方法
Future<bool> _onBackPressed() {
return showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('确定退出程序吗?'),
actions: <Widget>[
FlatButton(
child: Text('暂不'),
onPressed: () => Navigator.pop(context, false),
),
FlatButton(
child: Text('确定'),
onPressed: () => Navigator.pop(context, true),
),
],
),
);
}