Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add callback for on modal dismiss with drag to be utilized in Navigator 2.0 #15

Merged
merged 1 commit into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions lib/src/wolt_modal_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class WoltModalSheet<T> extends StatefulWidget {
required this.pageListBuilderNotifier,
required this.pageIndexNotifier,
required this.onModalDismissedWithBarrierTap,
required this.onModalDismissedWithDrag,
required this.decorator,
required this.modalTypeBuilder,
required this.animationController,
Expand All @@ -39,6 +40,7 @@ class WoltModalSheet<T> extends StatefulWidget {
final ValueNotifier<WoltModalSheetPageListBuilder> pageListBuilderNotifier;
final ValueNotifier<int> pageIndexNotifier;
final VoidCallback? onModalDismissedWithBarrierTap;
final VoidCallback? onModalDismissedWithDrag;
final Widget Function(Widget)? decorator;
final WoltModalType Function(BuildContext context) modalTypeBuilder;
final AnimationController? animationController;
Expand Down Expand Up @@ -68,6 +70,7 @@ class WoltModalSheet<T> extends StatefulWidget {
RouteSettings? routeSettings,
Duration? transitionDuration,
VoidCallback? onModalDismissedWithBarrierTap,
VoidCallback? onModalDismissedWithDrag,
AnimationController? transitionAnimationController,
AnimatedWidget? bottomSheetTransitionAnimation,
AnimatedWidget? dialogTransitionAnimation,
Expand Down Expand Up @@ -112,6 +115,7 @@ class WoltModalSheet<T> extends StatefulWidget {
RouteSettings? routeSettings,
Duration? transitionDuration,
VoidCallback? onModalDismissedWithBarrierTap,
VoidCallback? onModalDismissedWithDrag,
AnimationController? transitionAnimationController,
AnimatedWidget? bottomSheetTransitionAnimation,
AnimatedWidget? dialogTransitionAnimation,
Expand All @@ -133,6 +137,7 @@ class WoltModalSheet<T> extends StatefulWidget {
barrierDismissible: barrierDismissible,
enableDragForBottomSheet: enableDragForBottomSheet,
onModalDismissedWithBarrierTap: onModalDismissedWithBarrierTap,
onModalDismissedWithDrag: onModalDismissedWithDrag,
transitionAnimationController: transitionAnimationController,
useSafeArea: useSafeArea,
bottomSheetTransitionAnimation: bottomSheetTransitionAnimation,
Expand Down Expand Up @@ -220,10 +225,7 @@ class _WoltModalSheetState extends State<WoltModalSheet> {
id: barrierLayoutId,
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
widget.onModalDismissedWithBarrierTap?.call();
Navigator.of(context).pop();
},
onTap: widget.onModalDismissedWithBarrierTap ?? Navigator.of(context).pop,
child: const SizedBox.expand(),
),
),
Expand Down Expand Up @@ -331,7 +333,12 @@ class _WoltModalSheetState extends State<WoltModalSheet> {
);

if (isClosing && widget.route.isCurrent) {
Navigator.pop(context);
final onModalDismissedWithDrag = widget.onModalDismissedWithDrag;
if (onModalDismissedWithDrag != null) {
onModalDismissedWithDrag();
} else {
Navigator.pop(context);
}
}
}
}
4 changes: 4 additions & 0 deletions lib/src/wolt_modal_sheet_route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class WoltModalSheetRoute<T> extends PageRoute<T> {
this.pageIndexNotifier,
this.decorator,
this.onModalDismissedWithBarrierTap,
this.onModalDismissedWithDrag,
bool? enableDragForBottomSheet,
bool? useSafeArea,
bool? barrierDismissible,
Expand Down Expand Up @@ -47,6 +48,8 @@ class WoltModalSheetRoute<T> extends PageRoute<T> {

final VoidCallback? onModalDismissedWithBarrierTap;

final VoidCallback? onModalDismissedWithDrag;

final bool _enableDragForBottomSheet;

final bool _useSafeArea;
Expand Down Expand Up @@ -100,6 +103,7 @@ class WoltModalSheetRoute<T> extends PageRoute<T> {
pageListBuilderNotifier: pageListBuilderNotifier,
modalTypeBuilder: modalTypeBuilder,
onModalDismissedWithBarrierTap: onModalDismissedWithBarrierTap,
onModalDismissedWithDrag: onModalDismissedWithDrag,
animationController: animationController,
enableDragForBottomSheet: _enableDragForBottomSheet,
useSafeArea: _useSafeArea,
Expand Down
11 changes: 10 additions & 1 deletion playground/lib/home/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,16 @@ class _HomeScreenState extends State<HomeScreen> {
context: context,
pageListBuilderNotifier: pageListBuilderNotifier,
modalTypeBuilder: _modalTypeBuilder,
onModalDismissedWithBarrierTap: () => pageIndexNotifier.value = 0,
onModalDismissedWithDrag: () {
debugPrint('Bottom sheet is dismissed with drag.');
Navigator.of(context).pop();
pageIndexNotifier.value = 0;
},
onModalDismissedWithBarrierTap: () {
debugPrint('Modal is dismissed with barrier tap.');
Navigator.of(context).pop();
pageIndexNotifier.value = 0;
},
maxDialogWidth: 560,
minDialogWidth: 400,
minPageHeight: 0.4,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ class PlaygroundRouterDelegate extends RouterDelegate<PlaygroundRouterConfigurat
pages: pages,
onPopPage: (route, result) {
if (!route.didPop(result)) return false;
if (route.settings.name == SheetPage.routeName) {
_cubit.closeSheet();
}
return true;
},
);
Expand Down
8 changes: 8 additions & 0 deletions playground_navigator2/lib/router/router_pages/sheet_page.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:playground_navigator2/bloc/router_cubit.dart';
import 'package:wolt_modal_sheet/wolt_modal_sheet.dart';

class SheetPage extends Page<void> {
Expand All @@ -21,6 +23,12 @@ class SheetPage extends Page<void> {
pageIndexNotifier: pageIndexNotifier,
modalTypeBuilder: modalTypeBuilder,
pageListBuilderNotifier: pageListBuilderNotifier,
onModalDismissedWithDrag: () {
context.read<RouterCubit>().closeSheet();
},
onModalDismissedWithBarrierTap: () {
context.read<RouterCubit>().closeSheet();
},
routeSettings: this,
);
}
Expand Down