Skip to content

Instantly share code, notes, and snippets.

@stevenosse
Created November 20, 2023 15:16
Show Gist options
  • Save stevenosse/1a661ddd4c6a3fc9b3e142668a7be4e4 to your computer and use it in GitHub Desktop.
Save stevenosse/1a661ddd4c6a3fc9b3e142668a7be4e4 to your computer and use it in GitHub Desktop.
LifecycleWatcher
import 'package:flutter/material.dart';
class LifecycleWatcher extends StatefulWidget {
const LifecycleWatcher({
super.key,
required this.child,
this.onResume,
this.onPause,
this.onInactive,
this.onHide,
this.noDetach,
this.onShow,
this.onRestart,
this.onStateChange,
});
final Widget child;
final VoidCallback? onResume;
final VoidCallback? onPause;
final VoidCallback? onInactive;
final VoidCallback? onHide;
final VoidCallback? noDetach;
final VoidCallback? onShow;
final VoidCallback? onRestart;
final ValueChanged<AppLifecycleState>? onStateChange;
@override
State<LifecycleWatcher> createState() => _LifecycleWatcherState();
}
class _LifecycleWatcherState extends State<LifecycleWatcher> {
late final AppLifecycleListener _appLifecycleListener;
@override
void initState() {
_setupListeners();
super.initState();
}
@override
void dispose() {
_appLifecycleListener.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return widget.child;
}
void _setupListeners() {
_appLifecycleListener = AppLifecycleListener(
onResume: widget.onResume,
onPause: widget.onPause,
onInactive: widget.onInactive,
onHide: widget.onHide,
onDetach: widget.noDetach,
onShow: widget.onShow,
onRestart: widget.onRestart,
onStateChange: widget.onStateChange,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment