lifecycle_event_handler.dart 792 B

1234567891011121314151617181920212223242526272829303132
  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. if (suspendingCallBack != null) {
  22. await suspendingCallBack!();
  23. }
  24. break;
  25. }
  26. }
  27. }