diff options
Diffstat (limited to 'lib/widgets/common/behaviour/pop/scope.dart')
| -rw-r--r-- | lib/widgets/common/behaviour/pop/scope.dart | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/lib/widgets/common/behaviour/pop/scope.dart b/lib/widgets/common/behaviour/pop/scope.dart new file mode 100644 index 0000000..ff750b9 --- /dev/null +++ b/lib/widgets/common/behaviour/pop/scope.dart @@ -0,0 +1,54 @@ +import 'package:collection/collection.dart'; +import 'package:flutter/widgets.dart'; + +// this widget combines multiple pop handlers with a guaranteed order +class AvesPopScope extends StatelessWidget { + final List<PopHandler> handlers; + final Widget child; + + const AvesPopScope({ + super.key, + required this.handlers, + required this.child, + }); + + @override + Widget build(BuildContext context) { + final blocker = handlers.firstWhereOrNull((v) => !v.canPop(context)); + return PopScope( + canPop: blocker == null, + onPopInvokedWithResult: (didPop, result) { + if (!didPop) { + blocker?.onPopBlocked(context); + } + }, + child: child, + ); + } +} + +abstract class PopHandler { + bool canPop(BuildContext context); + + void onPopBlocked(BuildContext context); +} + +class APopHandler implements PopHandler { + final bool Function(BuildContext context) _canPop; + final void Function(BuildContext context) _onPopBlocked; + + APopHandler({ + required bool Function(BuildContext context) canPop, + required void Function(BuildContext context) onPopBlocked, + }) : _canPop = canPop, + _onPopBlocked = onPopBlocked; + + @override + bool canPop(BuildContext context) => _canPop(context); + + @override + void onPopBlocked(BuildContext context) => _onPopBlocked(context); +} + +@immutable +class PopExitNotification extends Notification {} |
