Skip to content

Commit

Permalink
Conditionally add gesture detector to page content
Browse files Browse the repository at this point in the history
  • Loading branch information
ulusoyca committed Jul 31, 2024
1 parent 6680510 commit bb8e363
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import 'package:flutter/material.dart';
import 'package:wolt_modal_sheet/wolt_modal_sheet.dart';

class WoltModalSheetContentGestureDetector extends StatelessWidget {
const WoltModalSheetContentGestureDetector({
class WoltModalSheetDragToDismissDetector extends StatelessWidget {
const WoltModalSheetDragToDismissDetector({
super.key,
required this.child,
required this.modalType,
required this.enableDrag,
required this.onModalDismissedWithDrag,
required this.modalContentKey,
required this.route,
});

final WoltModalType modalType;
final Widget child;
final bool enableDrag;
final WoltModalSheetRoute route;
final VoidCallback? onModalDismissedWithDrag;
final GlobalKey modalContentKey;
Expand All @@ -26,11 +24,6 @@ class WoltModalSheetContentGestureDetector extends StatelessWidget {

double get _minFlingVelocity => modalType.minFlingVelocity;

bool get canDragToDismiss =>
enableDrag &&
_dismissDirection != null &&
_dismissDirection != WoltModalDismissDirection.none;

bool get _isDismissUnderway =>
_animationController.status == AnimationStatus.reverse;

Expand All @@ -45,40 +38,41 @@ class WoltModalSheetContentGestureDetector extends StatelessWidget {

@override
Widget build(BuildContext context) {
final isVertical = _dismissDirection?.isVertical ?? false;
final isHorizontal = _dismissDirection?.isHorizontal ?? false;
final isVerticalDismissAllowed = _dismissDirection?.isVertical ?? false;
final isHorizontalDismissAllowed = _dismissDirection?.isHorizontal ?? false;

return NotificationListener(
onNotification: (notification) {
if (notification is OverscrollNotification &&
notification.dragDetails != null &&
canDragToDismiss) {
isVertical
? _handleVerticalDragUpdate(notification.dragDetails!)
: _handleHorizontalDragUpdate(context, notification.dragDetails!);
_handleVerticalDragUpdate(notification.dragDetails!);
notification.dragDetails != null) {
if (isVerticalDismissAllowed) {
_handleVerticalDragUpdate(notification.dragDetails!);
} else if (isHorizontalDismissAllowed) {
_handleHorizontalDragUpdate(context, notification.dragDetails!);
}
}
if (notification is ScrollEndNotification &&
notification.dragDetails != null &&
canDragToDismiss) {
isVertical
? _handleVerticalDragEnd(context, notification.dragDetails!)
: _handleHorizontalDragEnd(context, notification.dragDetails!);
notification.dragDetails != null) {
if (isVerticalDismissAllowed) {
_handleVerticalDragEnd(context, notification.dragDetails!);
} else if (isHorizontalDismissAllowed) {
_handleHorizontalDragEnd(context, notification.dragDetails!);
}
}
return true;
},
child: GestureDetector(
excludeFromSemantics: true,
onVerticalDragUpdate: (details) => canDragToDismiss && isVertical
onVerticalDragUpdate: (details) => isVerticalDismissAllowed
? _handleVerticalDragUpdate(details)
: null,
onVerticalDragEnd: (details) => canDragToDismiss && isVertical
onVerticalDragEnd: (details) => isVerticalDismissAllowed
? _handleVerticalDragEnd(context, details)
: null,
onHorizontalDragUpdate: (details) => canDragToDismiss && isHorizontal
onHorizontalDragUpdate: (details) => isHorizontalDismissAllowed
? _handleHorizontalDragUpdate(context, details)
: null,
onHorizontalDragEnd: (details) => canDragToDismiss && isHorizontal
onHorizontalDragEnd: (details) => isHorizontalDismissAllowed
? _handleHorizontalDragEnd(context, details)
: null,
child: child,
Expand Down
61 changes: 32 additions & 29 deletions lib/src/wolt_modal_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:wolt_modal_sheet/src/content/wolt_modal_sheet_animated_switcher.
import 'package:wolt_modal_sheet/src/theme/wolt_modal_sheet_default_theme_data.dart';
import 'package:wolt_modal_sheet/src/utils/wolt_modal_type_utils.dart';
import 'package:wolt_modal_sheet/src/widgets/wolt_animated_modal_barrier.dart';
import 'package:wolt_modal_sheet/src/widgets/wolt_modal_sheet_content_gesture_detector.dart';
import 'package:wolt_modal_sheet/src/widgets/wolt_modal_sheet_drag_to_dismiss_detector.dart';
import 'package:wolt_modal_sheet/wolt_modal_sheet.dart';

const int defaultWoltModalTransitionAnimationDuration = 350;
Expand Down Expand Up @@ -357,39 +357,42 @@ class WoltModalSheetState extends State<WoltModalSheet> {
key: _childKey,
child: Semantics(
label: modalType.routeLabel(context),
child: WoltModalSheetContentGestureDetector(
route: widget.route,
enableDrag: enableDrag,
modalContentKey: _childKey,
onModalDismissedWithDrag: widget.onModalDismissedWithDrag,
modalType: modalType,
child: Material(
color: pageBackgroundColor,
elevation: modalElevation,
surfaceTintColor: surfaceTintColor,
shadowColor: shadowColor,
shape: modalType.shapeBorder,
clipBehavior: clipBehavior,
child: LayoutBuilder(
builder: (_, constraints) {
return modalType.decoratePageContent(
context,
WoltModalSheetAnimatedSwitcher(
woltModalType: modalType,
pageIndex: currentPageIndex,
pages: pages,
sheetWidth: constraints.maxWidth,
showDragHandle: showDragHandle,
),
useSafeArea,
);
},
),
child: Material(
color: pageBackgroundColor,
elevation: modalElevation,
surfaceTintColor: surfaceTintColor,
shadowColor: shadowColor,
shape: modalType.shapeBorder,
clipBehavior: clipBehavior,
child: LayoutBuilder(
builder: (_, constraints) {
return modalType.decoratePageContent(
context,
WoltModalSheetAnimatedSwitcher(
woltModalType: modalType,
pageIndex: currentPageIndex,
pages: pages,
sheetWidth: constraints.maxWidth,
showDragHandle: showDragHandle,
),
useSafeArea,
);
},
),
),
),
);

if (enableDrag) {
pageContent = WoltModalSheetDragToDismissDetector(
route: widget.route,
modalContentKey: _childKey,
onModalDismissedWithDrag: widget.onModalDismissedWithDrag,
modalType: modalType,
child: pageContent,
);
}

final multiChildLayout = CustomMultiChildLayout(
delegate: _WoltModalMultiChildLayoutDelegate(
contentLayoutId: contentLayoutId,
Expand Down

0 comments on commit bb8e363

Please sign in to comment.