30 lines
779 B
Dart
30 lines
779 B
Dart
import 'package:flutter/material.dart';
|
|
|
|
class AppLifecycleLockObserver extends WidgetsBindingObserver {
|
|
final VoidCallback onLock;
|
|
final VoidCallback? onBackground;
|
|
final VoidCallback? onForeground;
|
|
final bool Function()? shouldLock;
|
|
|
|
AppLifecycleLockObserver({
|
|
required this.onLock,
|
|
this.onBackground,
|
|
this.onForeground,
|
|
this.shouldLock,
|
|
});
|
|
|
|
@override
|
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
|
switch (state) {
|
|
case AppLifecycleState.inactive:
|
|
case AppLifecycleState.hidden:
|
|
case AppLifecycleState.paused:
|
|
case AppLifecycleState.detached:
|
|
if (shouldLock?.call() ?? true) onLock();
|
|
onBackground?.call();
|
|
case AppLifecycleState.resumed:
|
|
onForeground?.call();
|
|
}
|
|
}
|
|
}
|