Flutter在主页面双击退出APP.
//定时器
Timer _timer;
//上次点击时间
DateTime _lastPressedAt;
//将Scaffold 作为WillPopScope的子控件
WillPopScope(
onWillPop: () async {
if (_lastPressedAt == null ||
DateTime.now().difference(_lastPressedAt) > Duration(seconds: 2)) {
//两次点击间隔超过1秒则重新计时
_lastPressedAt = DateTime.now();
showToast('连续按两次返回键返回桌面', position: ToastPosition.bottom);
return false;
} else {
exit(0);
_timer.cancel();
return true;
}
},
child: Scaffold(
appBar: appbar(),
body: body(),
),
);