Skip to content

Commit

Permalink
feat(controller): add method to jump to index without animation (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardodalarme committed Jan 28, 2024
1 parent a79449d commit 40ca68c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

## [7.0.0]

- Adds `moveTo` method to `CardSwiperController` to move to a specific card index.
- **BREAKING CHANGE**:
- Upgrade min dart sdk to 3.0.0
- Replace `swipe`, `swipeLeft`, `swipeRight`, `swipeUp`, `swipeDown` with `swipe(CardSwiperDirection direction)`
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ The `Controller` is used to swipe the card from outside of the widget. You can c
| ----------- | :--------------------------------------------- |
| swipe | Swipes the card to a specific direction. |
| undo | Bring back the last card that was swiped away. |
| moveTo | Change the top card to a specific index. |

<hr/>

Expand Down
7 changes: 6 additions & 1 deletion lib/src/controller/card_swiper_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ class CardSwiperController {
_eventController.add(const ControllerUndoEvent());
}

// Change the top card to a specific index.
void moveTo(int index) {
_eventController.add(ControllerMoveEvent(index));
}

Future<void> dispose() async {
await _eventController.close();
await _eventController.close();
}
}
7 changes: 6 additions & 1 deletion lib/src/controller/controller_event.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:flutter_card_swiper/flutter_card_swiper.dart';

abstract class ControllerEvent {
sealed class ControllerEvent {
const ControllerEvent();
}

Expand All @@ -13,3 +13,8 @@ class ControllerSwipeEvent extends ControllerEvent {
class ControllerUndoEvent extends ControllerEvent {
const ControllerUndoEvent();
}

class ControllerMoveEvent extends ControllerEvent {
final int index;
const ControllerMoveEvent(this.index);
}
11 changes: 10 additions & 1 deletion lib/src/widget/card_swiper_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper>
return switch (event) {
ControllerSwipeEvent(:final direction) => _swipe(direction),
ControllerUndoEvent() => _undo(),
_ => null
ControllerMoveEvent(:final index) => _moveTo(index),
};
}

Expand Down Expand Up @@ -281,6 +281,15 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper>
_cardAnimation.animateUndo(context, direction);
}

void _moveTo(int index) {
if (index == _currentIndex) return;
if (index < 0 || index >= widget.cardsCount) return;

setState(() {
_undoableIndex.state = index;
});
}

int numberOfCardsOnScreen() {
if (widget.isLoop) {
return widget.numberOfCardsDisplayed;
Expand Down

0 comments on commit 40ca68c

Please sign in to comment.