Created
November 20, 2023 15:16
-
-
Save stevenosse/1a661ddd4c6a3fc9b3e142668a7be4e4 to your computer and use it in GitHub Desktop.
LifecycleWatcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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