double_tap_back_exit_app.dart
1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:Parlando/util/toast_utils.dart';
/// 双击返回退出
class DoubleTapBackExitApp extends StatefulWidget {
const DoubleTapBackExitApp({
Key? key,
required this.child,
this.duration = const Duration(milliseconds: 2500),
}): super(key: key);
final Widget child;
/// 两次点击返回按钮的时间间隔
final Duration duration;
@override
_DoubleTapBackExitAppState createState() => _DoubleTapBackExitAppState();
}
class _DoubleTapBackExitAppState extends State<DoubleTapBackExitApp> {
DateTime? _lastTime;
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: _isExit,
child: widget.child,
);
}
Future<bool> _isExit() async {
if (_lastTime == null || DateTime.now().difference(_lastTime!) > widget.duration) {
_lastTime = DateTime.now();
Toast.show('再次点击退出应用');
return Future.value(false);
}
Toast.cancelToast();
/// 不推荐使用 `dart:io` 的 exit(0)
await SystemNavigator.pop();
return Future.value(true);
}
}