@@ -14,6 +14,8 @@ import 'package:firebase_core/firebase_core.dart';
1414import 'package:firebase_crashlytics/firebase_crashlytics.dart' ;
1515import 'package:flutter/cupertino.dart' ;
1616import 'package:flutter/foundation.dart' ;
17+ import 'package:flutter/material.dart' ;
18+ import 'package:flutter/scheduler.dart' ;
1719import 'package:flutter_localizations/flutter_localizations.dart' ;
1820import 'package:flutter_smart_dialog/flutter_smart_dialog.dart' ;
1921import 'package:flutter_windowmanager/flutter_windowmanager.dart' ;
@@ -116,8 +118,10 @@ Future<void> main() async {
116118}
117119
118120class MyApp extends StatefulWidget {
121+ const MyApp ({super .key});
122+
119123 @override
120- _MyAppState createState () => _MyAppState ();
124+ State < MyApp > createState () => _MyAppState ();
121125}
122126
123127class _MyAppState extends State <MyApp > with WidgetsBindingObserver {
@@ -126,11 +130,51 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
126130 final EhSettingService _ehSettingService = Get .find ();
127131 final AutoLockController _autoLockController = Get .find ();
128132
133+ late final AppLifecycleListener _listener;
134+ late AppLifecycleState ? _state;
135+
129136 @override
130137 void initState () {
131138 super .initState ();
132139 WidgetsBinding .instance.addObserver (this );
133- _autoLockController.resumed ();
140+ _autoLockController.checkLock ();
141+
142+ _state = SchedulerBinding .instance.lifecycleState;
143+ _listener = AppLifecycleListener (
144+ onShow: () {
145+ _handleTransition ('show' );
146+ // Get.toNamed(EHRoutes.unlockPage);
147+ // _autoLockController.checkLock();
148+ },
149+ onResume: () async {
150+ _handleTransition ('resume' );
151+
152+ await _autoLockController.checkLock ();
153+
154+ // resumed 时清除 FLAG_SECURE ,避免无法截屏
155+ FlutterWindowManager .clearFlags (FlutterWindowManager .FLAG_SECURE );
156+
157+ if (context.mounted) {
158+ _ehSettingService.chkClipboardLink (context);
159+ }
160+ },
161+ onHide: () {
162+ _handleTransition ('hide' );
163+ _autoLockController.paused ();
164+ },
165+ onInactive: () {
166+ _handleTransition ('inactive' );
167+ // 添加 FLAG_SECURE
168+ _ehSettingService.applyBlurredInRecentTasks ();
169+ },
170+ onPause: () => _handleTransition ('pause' ),
171+ onDetach: () => _handleTransition ('detach' ),
172+ onRestart: () => _handleTransition ('restart' ),
173+ );
174+ }
175+
176+ void _handleTransition (String name) {
177+ logger.d ('########################## main $name ' );
134178 }
135179
136180 @override
@@ -145,34 +189,6 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
145189 View .of (context).platformDispatcher.platformBrightness;
146190 }
147191
148- @override
149- void didChangeAppLifecycleState (AppLifecycleState state) {
150- super .didChangeAppLifecycleState (state);
151- logger.t ('state: $state ' );
152- _autoLockController.updateStat (state);
153- if (state != AppLifecycleState .resumed) {
154- logger.d ('applyBlurredInRecentTasks' );
155-
156- // 非 resumed 时根据设置 添加 FLAG_SECURE
157- _ehSettingService.applyBlurredInRecentTasks ();
158- }
159- if (state == AppLifecycleState .paused) {
160- // went to Background
161- // loggerTime.d('paused');
162- _autoLockController.paused ();
163- }
164- if (state == AppLifecycleState .resumed) {
165- // came back to Foreground
166- // loggerTime.d('resumed');
167- _autoLockController.resumed ();
168-
169- // resumed 时清除 FLAG_SECURE ,避免无法截屏
170- FlutterWindowManager .clearFlags (FlutterWindowManager .FLAG_SECURE );
171-
172- _ehSettingService.chkClipboardLink (context);
173- }
174- }
175-
176192 @override
177193 Widget build (BuildContext context) {
178194 Widget cupertinoApp () {
@@ -218,7 +234,7 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
218234 initialRoute: EHRoutes .root,
219235 theme: themeService.themeData,
220236 locale: localeService.locale,
221- enableLog: false && kDebugMode ,
237+ enableLog: false ,
222238 logWriterCallback: loggerGetx,
223239 supportedLocales: < Locale > [
224240 ...L10n .delegate.supportedLocales,
@@ -232,12 +248,11 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
232248 ],
233249
234250 localeResolutionCallback: (_, Iterable <Locale > supportedLocales) {
235- final Locale _locale = PlatformDispatcher .instance.locale;
251+ final Locale locale = PlatformDispatcher .instance.locale;
236252
237253 return localeService.locale ??
238254 supportedLocales.firstWhere (
239- (Locale locale) =>
240- locale.languageCode == _locale.languageCode,
255+ (Locale sl) => sl.languageCode == locale.languageCode,
241256 orElse: () => supportedLocales.first,
242257 );
243258 },
0 commit comments