Skip to content

Commit

Permalink
add ability to rewind till the end
Browse files Browse the repository at this point in the history
  • Loading branch information
maxzod committed Sep 11, 2022
1 parent 8896450 commit f06b9c9
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 53 deletions.
2 changes: 1 addition & 1 deletion example/android/app/build.gradle
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
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.3.50'
ext.kotlin_version = '1.6.10'
repositories {
google()
jcenter()
Expand Down
31 changes: 12 additions & 19 deletions example/pubspec.lock
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
13 changes: 5 additions & 8 deletions lib/src/swipable_stack.dart
Expand Up @@ -512,12 +512,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 +583,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
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
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

0 comments on commit f06b9c9

Please sign in to comment.