Skip to content

Instantly share code, notes, and snippets.

@HansMuller
Created February 7, 2023 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HansMuller/94745abd6fb880c40dfc188cdc6c7fc2 to your computer and use it in GitHub Desktop.
Save HansMuller/94745abd6fb880c40dfc188cdc6c7fc2 to your computer and use it in GitHub Desktop.
import 'package:flutter/widgets.dart';
class Application with WidgetsBindingObserver {
final Widget home;
final Function onLaunch;
final Function onTerminate;
Application({
@required this.home,
this.onLaunch,
this.onTerminate,
}) {
WidgetsBinding.instance.addObserver(this);
}
void run() {
runApp(home);
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.initiated:
if (onLaunch != null) {
onLaunch();
}
break;
case AppLifecycleState.paused:
case AppLifecycleState.inactive:
case AppLifecycleState.detached:
if (onTerminate != null) {
onTerminate();
}
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment