Skip to content
Open
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
2 changes: 1 addition & 1 deletion example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 30
compileSdkVersion 31

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
Expand Down
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.3.50'
ext.kotlin_version = '1.6.10'
repositories {
google()
jcenter()
Expand Down
13 changes: 10 additions & 3 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
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
31 changes: 12 additions & 19 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.8.2"
version: "2.9.0"
boolean_selector:
dependency: transitive
description:
Expand All @@ -21,21 +21,14 @@ packages:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.1"
version: "1.2.1"
clock:
dependency: transitive
description:
name: clock
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.1.1"
collection:
dependency: transitive
description:
Expand All @@ -49,7 +42,7 @@ packages:
name: fake_async
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "1.3.1"
flutter:
dependency: "direct main"
description: flutter
Expand Down Expand Up @@ -80,28 +73,28 @@ packages:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.11"
version: "0.12.12"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.4"
version: "0.1.5"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.7.0"
version: "1.8.0"
path:
dependency: transitive
description:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.1"
version: "1.8.2"
pedantic_mono:
dependency: "direct dev"
description:
Expand All @@ -120,7 +113,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.2"
version: "1.9.0"
sprung:
dependency: transitive
description:
Expand Down Expand Up @@ -148,7 +141,7 @@ packages:
name: string_scanner
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.1.1"
swipable_stack:
dependency: "direct main"
description:
Expand All @@ -162,14 +155,14 @@ packages:
name: term_glyph
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.2.1"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.9"
version: "0.4.12"
vector_math:
dependency: transitive
description:
Expand Down
18 changes: 14 additions & 4 deletions lib/src/model/swipe_rate_per_threshold.dart
Original file line number Diff line number Diff line change
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(directionRateForbid.direction);
}

return null;
} else {
return directionRate.direction;
Expand Down
22 changes: 14 additions & 8 deletions lib/src/swipable_stack.dart
Original file line number Diff line number Diff line change
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 Expand Up @@ -512,12 +521,11 @@ class _SwipableStackState extends State<SwipableStack>
swipeProgress: swipeDirectionRate?.rate ?? 0.0,
),
);
final previousSession = widget.controller._previousSession;
if (previousSession != null) {
if (widget.controller.history.isNotEmpty) {
cards.add(
_SwipablePositioned(
key: child.key ?? ValueKey(rewindTargetIndex),
session: previousSession,
session: widget.controller.history.last,
index: -1,
viewFraction: widget.viewFraction,
swipeAnchor: widget.swipeAnchor,
Expand Down Expand Up @@ -584,13 +592,11 @@ class _SwipableStackState extends State<SwipableStack>
void _rewind({
required Duration duration,
}) {
if (!canAnimationStart) {
return;
}
final previousSession = widget.controller._previousSession;
if (previousSession == null) {
if (widget.controller.history.isEmpty || !canAnimationStart) {
return;
}
final previousSession = widget.controller.history.last;

widget.controller._prepareRewind();
_rewindAnimationController.duration = duration;
final rewindAnimation = _rewindAnimationController.tweenCurvedAnimation(
Expand Down
10 changes: 5 additions & 5 deletions lib/src/swipable_stack_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ class SwipableStackController extends ChangeNotifier {

void _initializeSessions() {
_currentSessionState = null;
_previousSession = null;
history.removeLast();
notifyListeners();
}

void _completeAction() {
_previousSession = currentSession?.copyWith();
history.add(currentSession!.copyWith());
_currentIndex += 1;
_currentSessionState = null;
notifyListeners();
Expand All @@ -57,15 +57,15 @@ class SwipableStackController extends ChangeNotifier {
}

void _prepareRewind() {
_currentSessionState = _previousSession?.copyWith();
_currentSessionState = history.last.copyWith();
_currentIndex -= 1;
notifyListeners();
}

_SwipableStackPosition? _previousSession;
final history = <_SwipableStackPosition>[];

/// Whether to rewind.
bool get canRewind => _previousSession != null && _currentIndex > 0;
bool get canRewind => history.isNotEmpty;

/// Advance to the next card with specified [swipeDirection].
///
Expand Down
31 changes: 12 additions & 19 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.8.2"
version: "2.9.0"
boolean_selector:
dependency: transitive
description:
Expand All @@ -21,21 +21,14 @@ packages:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.1"
version: "1.2.1"
clock:
dependency: transitive
description:
name: clock
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.1.1"
collection:
dependency: transitive
description:
Expand All @@ -49,7 +42,7 @@ packages:
name: fake_async
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "1.3.1"
flutter:
dependency: "direct main"
description: flutter
Expand Down Expand Up @@ -80,28 +73,28 @@ packages:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.11"
version: "0.12.12"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.4"
version: "0.1.5"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.7.0"
version: "1.8.0"
path:
dependency: transitive
description:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.1"
version: "1.8.2"
pedantic_mono:
dependency: "direct dev"
description:
Expand All @@ -120,7 +113,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.2"
version: "1.9.0"
sprung:
dependency: "direct main"
description:
Expand Down Expand Up @@ -148,21 +141,21 @@ packages:
name: string_scanner
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.1.1"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.2.1"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.9"
version: "0.4.12"
vector_math:
dependency: transitive
description:
Expand Down