import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.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 { DateTime? _lastTime; @override Widget build(BuildContext context) { return WillPopScope( onWillPop: _isExit, child: widget.child, ); } Future _isExit() async { if (_lastTime == null || DateTime.now().difference(_lastTime!) > widget.duration) { _lastTime = DateTime.now(); SmartDialog.showToast("Click again and exit the app"); return Future.value(false); } // ToastUtil.cancelToast(); await SystemNavigator.pop(); return Future.value(true); } }