28 lines
910 B
Dart
28 lines
910 B
Dart
|
|
import 'package:flutter/widgets.dart';
|
||
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||
|
|
|
||
|
|
/// Provides the current AppLifecycleState.
|
||
|
|
/// Using this instead of a periodic timer ensures we only re-check time-based logic
|
||
|
|
/// when the user actually comes back to the app (battery efficient).
|
||
|
|
///
|
||
|
|
/// This is a simple notifier that updates whenever the app lifecycle changes.
|
||
|
|
class AppLifecycleNotifier extends Notifier<AppLifecycleState> with WidgetsBindingObserver {
|
||
|
|
@override
|
||
|
|
AppLifecycleState build() {
|
||
|
|
WidgetsBinding.instance.addObserver(this);
|
||
|
|
ref.onDispose(() {
|
||
|
|
WidgetsBinding.instance.removeObserver(this);
|
||
|
|
});
|
||
|
|
return AppLifecycleState.resumed;
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||
|
|
this.state = state;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
final appLifecycleProvider = NotifierProvider<AppLifecycleNotifier, AppLifecycleState>(
|
||
|
|
AppLifecycleNotifier.new,
|
||
|
|
);
|