aves/lib/widgets/common/behaviour/pop/scope.dart
2023-11-29 19:48:28 +01:00

30 lines
775 B
Dart

import 'package:flutter/widgets.dart';
// as of Flutter v3.3.10, the resolution order of multiple `WillPopScope` is random
// so this widget combines multiple handlers with a guaranteed order
class AvesPopScope extends StatelessWidget {
final List<bool Function(BuildContext context)> handlers;
final Widget child;
const AvesPopScope({
super.key,
required this.handlers,
required this.child,
});
@override
Widget build(BuildContext context) {
return PopScope(
canPop: false,
onPopInvoked: (didPop) {
if (didPop) return;
final shouldPop = handlers.fold(true, (prev, v) => prev ? v(context) : false);
if (shouldPop) {
Navigator.of(context).pop();
}
},
child: child,
);
}
}