feat: 两次退出确认
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
@ -53,6 +54,7 @@ class MainController extends GetxController {
|
||||
final StreamController<bool> bottomBarStream =
|
||||
StreamController<bool>.broadcast();
|
||||
Box setting = GStrorage.setting;
|
||||
DateTime? _lastPressedAt;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
@ -61,4 +63,16 @@ class MainController extends GetxController {
|
||||
Utils.checkUpdata();
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> onBackPressed(BuildContext context) {
|
||||
if (_lastPressedAt == null ||
|
||||
DateTime.now().difference(_lastPressedAt!) >
|
||||
const Duration(seconds: 2)) {
|
||||
// 两次点击时间间隔超过2秒,重新记录时间戳
|
||||
_lastPressedAt = DateTime.now();
|
||||
SmartDialog.showToast("再按一次退出Pili");
|
||||
return Future.value(false); // 不退出应用
|
||||
}
|
||||
return Future.value(true); // 退出应用
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user