You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.1 KiB
39 lines
1.1 KiB
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
|
|
|
|
class DoubleTapToExitWidget extends StatefulWidget {
|
|
final Widget child;
|
|
final Duration duration;
|
|
|
|
const DoubleTapToExitWidget(
|
|
{super.key,
|
|
required this.child,
|
|
this.duration = const Duration(seconds: 1)});
|
|
|
|
@override
|
|
State<DoubleTapToExitWidget> createState() => DoubleTapToExitWidgetState();
|
|
}
|
|
|
|
class DoubleTapToExitWidgetState extends State<DoubleTapToExitWidget> {
|
|
DateTime? lastPressedAt;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return PopScope(
|
|
canPop: false,
|
|
onPopInvokedWithResult: (bool didPop, Object? result) async {
|
|
final now = DateTime.now();
|
|
if (lastPressedAt == null ||
|
|
now.difference(lastPressedAt!) >= widget.duration) {
|
|
setState(() {
|
|
lastPressedAt = now;
|
|
});
|
|
SmartDialog.showToast('再按一次退出');
|
|
} else {
|
|
SystemNavigator.pop();
|
|
}
|
|
},
|
|
child: widget.child);
|
|
}
|
|
}
|