Flutter_04_实现连续两次返回退出

/// 封装一个连续按两次返回键退出页面的组件
/// child 子组件
/// delay 两次返回间隔时间
class UseWillPopScope extends StatefulWidget {
  final Widget child;
  final Duration delay;

  const UseWillPopScope(
      {Key? key, this.delay = const Duration(seconds: 1), required this.child})
      : super(key: key);

  @override
  _UseWillPopScopeState createState() => _UseWillPopScopeState();
}

class _UseWillPopScopeState extends State<UseWillPopScope> {
  DateTime? _lastWillPopAt;

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      child: widget.child,
      onWillPop: () async {
        print(_lastWillPopAt);
        if (_lastWillPopAt == null ||
            _lastWillPopAt != null &&
                DateTime.now().difference(_lastWillPopAt!) > widget.delay) {
          _lastWillPopAt = DateTime.now();
          Fluttertoast.showToast(
            msg: '再按一次退出Flutter Go',
            fontSize: 16.0,
            backgroundColor: Colors.grey,
            gravity: ToastGravity.CENTER,
          );
          return false;
        }
        return true;
      },
    );
  }
}

原文地址:https://www.cnblogs.com/carp-li/p/15108031.html