31 lines
827 B
Dart
31 lines
827 B
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/widgets.dart';
|
|
|
|
class LifecycleEventHandler extends WidgetsBindingObserver {
|
|
final AsyncCallback? resumeCallBack;
|
|
final AsyncCallback? suspendingCallBack;
|
|
|
|
LifecycleEventHandler({
|
|
this.resumeCallBack,
|
|
this.suspendingCallBack,
|
|
});
|
|
|
|
@override
|
|
Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
|
|
switch (state) {
|
|
case AppLifecycleState.resumed:
|
|
if (resumeCallBack != null) {
|
|
await resumeCallBack!();
|
|
}
|
|
break;
|
|
case AppLifecycleState.inactive:
|
|
case AppLifecycleState.paused:
|
|
case AppLifecycleState.detached:
|
|
case AppLifecycleState.hidden:
|
|
if (suspendingCallBack != null) {
|
|
await suspendingCallBack!();
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|