Skip to content

Commit

Permalink
add onSwipeForbiden
Browse files Browse the repository at this point in the history
  • Loading branch information
maxzod committed Sep 11, 2022
1 parent f06b9c9 commit 7dbdd9e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
13 changes: 10 additions & 3 deletions example/lib/main.dart
Expand Up @@ -68,14 +68,21 @@ class _HomeState extends State<Home> {
padding: const EdgeInsets.all(8),
child: SwipableStack(
detectableSwipeDirections: const {
SwipeDirection.right,
SwipeDirection.left,
// SwipeDirection.right,
// SwipeDirection.left,
SwipeDirection.up,
SwipeDirection.down,
},
controller: _controller,
stackClipBehaviour: Clip.none,
onSwipeForbiden: (index, direction) {
if (kDebugMode) {
print('onSwipeForbiden $index, $direction');
}
},
onSwipeCompleted: (index, direction) {
if (kDebugMode) {
print('$index, $direction');
print('onSwipeCompleted $index, $direction');
}
},
horizontalSwipeThreshold: 0.8,
Expand Down
18 changes: 14 additions & 4 deletions lib/src/model/swipe_rate_per_threshold.dart
Expand Up @@ -94,17 +94,27 @@ extension _SwipableStackPositionX on _SwipableStackPosition {
required double horizontalSwipeThreshold,
required double verticalSwipeThreshold,
required Set<SwipeDirection> detectableDirections,
required ValueChanged<SwipeDirection> isForbidden,
}) {
final directionRate = swipeDirectionRate(
constraints: constraints,
horizontalSwipeThreshold: horizontalSwipeThreshold,
verticalSwipeThreshold: verticalSwipeThreshold,
detectableDirections: detectableDirections,
);
if (directionRate == null) {
return null;
}
if (directionRate.rate < 1) {

if (directionRate == null || directionRate.rate < 1) {
final directionRateForbid = swipeDirectionRate(
constraints: constraints,
horizontalSwipeThreshold: horizontalSwipeThreshold,
verticalSwipeThreshold: verticalSwipeThreshold,
detectableDirections: SwipeDirection.values.toSet(),
);

if (directionRateForbid != null && directionRateForbid.rate >= 1) {
isForbidden(directionRate!.direction);
}

return null;
} else {
return directionRate.direction;
Expand Down
9 changes: 9 additions & 0 deletions lib/src/swipable_stack.dart
Expand Up @@ -22,6 +22,7 @@ class SwipableStack extends StatefulWidget {
required this.builder,
SwipableStackController? controller,
this.onSwipeCompleted,
this.onSwipeForbiden,
this.onWillMoveNext,
this.overlayBuilder,
this.horizontalSwipeThreshold = _defaultHorizontalSwipeThreshold,
Expand Down Expand Up @@ -58,6 +59,7 @@ class SwipableStack extends StatefulWidget {

/// Callback called when the Swipe is completed.
final SwipeCompletionCallback? onSwipeCompleted;
final SwipeCompletionCallback? onSwipeForbiden;

/// Callback called just before launching the Swipe action.
///
Expand Down Expand Up @@ -412,6 +414,12 @@ class _SwipableStackState extends State<SwipableStack>
horizontalSwipeThreshold: widget.horizontalSwipeThreshold,
verticalSwipeThreshold: widget.verticalSwipeThreshold,
detectableDirections: widget.detectableSwipeDirections,
isForbidden: (dir) {
widget.onSwipeForbiden?.call(
_currentIndex,
dir,
);
},
);

if (swipeAssistDirection == null) {
Expand All @@ -425,6 +433,7 @@ class _SwipableStackState extends State<SwipableStack>
true;
if (!allowMoveNext) {
_cancelSwipe();

return;
}
_swipeNext(swipeAssistDirection);
Expand Down

0 comments on commit 7dbdd9e

Please sign in to comment.