fix: 两次返回退出应用 issues #303
This commit is contained in:
@ -1,5 +1,6 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
|
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
|
||||||
import 'package:get/get.dart';
|
import 'package:get/get.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
@ -66,15 +67,15 @@ class MainController extends GetxController {
|
|||||||
hideTabBar = setting.get(SettingBoxKey.hideTabBar, defaultValue: true);
|
hideTabBar = setting.get(SettingBoxKey.hideTabBar, defaultValue: true);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<bool> onBackPressed(BuildContext context) {
|
void onBackPressed(BuildContext context) {
|
||||||
if (_lastPressedAt == null ||
|
if (_lastPressedAt == null ||
|
||||||
DateTime.now().difference(_lastPressedAt!) >
|
DateTime.now().difference(_lastPressedAt!) >
|
||||||
const Duration(seconds: 2)) {
|
const Duration(seconds: 2)) {
|
||||||
// 两次点击时间间隔超过2秒,重新记录时间戳
|
// 两次点击时间间隔超过2秒,重新记录时间戳
|
||||||
_lastPressedAt = DateTime.now();
|
_lastPressedAt = DateTime.now();
|
||||||
SmartDialog.showToast("再按一次退出Pili");
|
SmartDialog.showToast("再按一次退出Pili");
|
||||||
return Future.value(false); // 不退出应用
|
return; // 不退出应用
|
||||||
}
|
}
|
||||||
return Future.value(true); // 退出应用
|
SystemNavigator.pop(); // 退出应用
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -94,7 +94,10 @@ class _MainAppState extends State<MainApp> with SingleTickerProviderStateMixin {
|
|||||||
localCache.put('sheetHeight', sheetHeight);
|
localCache.put('sheetHeight', sheetHeight);
|
||||||
localCache.put('statusBarHeight', statusBarHeight);
|
localCache.put('statusBarHeight', statusBarHeight);
|
||||||
return PopScope(
|
return PopScope(
|
||||||
onPopInvoked: (bool status) => _mainController.onBackPressed(context),
|
canPop: false,
|
||||||
|
onPopInvoked: (bool didPop) async {
|
||||||
|
_mainController.onBackPressed(context);
|
||||||
|
},
|
||||||
child: Scaffold(
|
child: Scaffold(
|
||||||
extendBody: true,
|
extendBody: true,
|
||||||
body: PageView(
|
body: PageView(
|
||||||
|
Reference in New Issue
Block a user