lifecycle_event_handler.dart 827 B

12345678910111213141516171819202122232425262728293031
  1. import 'package:flutter/foundation.dart';
  2. import 'package:flutter/widgets.dart';
  3. class LifecycleEventHandler extends WidgetsBindingObserver {
  4. final AsyncCallback? resumeCallBack;
  5. final AsyncCallback? suspendingCallBack;
  6. LifecycleEventHandler({
  7. this.resumeCallBack,
  8. this.suspendingCallBack,
  9. });
  10. @override
  11. Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
  12. switch (state) {
  13. case AppLifecycleState.resumed:
  14. if (resumeCallBack != null) {
  15. await resumeCallBack!();
  16. }
  17. break;
  18. case AppLifecycleState.inactive:
  19. case AppLifecycleState.paused:
  20. case AppLifecycleState.detached:
  21. case AppLifecycleState.hidden:
  22. if (suspendingCallBack != null) {
  23. await suspendingCallBack!();
  24. }
  25. break;
  26. }
  27. }
  28. }