Skip to content

Commit

Permalink
feat: Added a lifecycle widget to handle lifecycle changes
Browse files Browse the repository at this point in the history
  • Loading branch information
vanlooverenkoen committed Dec 24, 2023
1 parent 0dd3485 commit 7bf5754
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/impaktfull_architecture.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ library impaktfull_architecture;

export 'src/util/connectivity/connectivity.dart';
export 'src/provider/change_notifier_ex.dart';
export 'src/widget/lifecycle/lifecycle_widget.dart';

// 3rd party packages
export 'package:connectivity_plus/connectivity_plus.dart';
Expand Down
77 changes: 77 additions & 0 deletions lib/src/widget/lifecycle/lifecycle_widget.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import 'package:flutter/material.dart';

/// Widget that handles lifecycle changes based on the [WidgetsBindingObserver]
/// And mapping the [AppLifecycleState] to a correct callback.
class LifecycleWidget extends StatefulWidget {
/// The widget below this widget in the tree.
final Widget child;

/// [onResumed] is triggered when the app goes to the [AppLifecycleState.resumed] state.
final VoidCallback? onResumed;

/// [onPaused] is triggered when the app goes to the [AppLifecycleState.paused] state.
final VoidCallback? onPaused;

/// [onInactive] is triggered when the app goes to the [AppLifecycleState.inactive] state.
final VoidCallback? onInactive;

/// [onHidden] is triggered when the app goes to the [AppLifecycleState.hidden] state.
final VoidCallback? onHidden;

/// [onDetached] is triggered when the app goes to the [AppLifecycleState.detached] state.
final VoidCallback? onDetached;

/// Widget that handles lifecycle changes based on the [WidgetsBindingObserver]
/// And mapping the [AppLifecycleState] to a correct callback.
const LifecycleWidget({
required this.child,
this.onResumed,
this.onPaused,
this.onInactive,
this.onHidden,
this.onDetached,
super.key,
});

@override
State<LifecycleWidget> createState() => _LifecycleWidgetState();
}

class _LifecycleWidgetState extends State<LifecycleWidget>
with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}

@override
Widget build(BuildContext context) => widget.child;

@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.resumed:
widget.onResumed?.call();
break;
case AppLifecycleState.paused:
widget.onPaused?.call();
break;
case AppLifecycleState.inactive:
widget.onInactive?.call();
break;
case AppLifecycleState.hidden:
widget.onHidden?.call();
break;
case AppLifecycleState.detached:
widget.onDetached?.call();
break;
}
}
}

0 comments on commit 7bf5754

Please sign in to comment.