diff --git a/CHANGELOG.md b/CHANGELOG.md index f5e559e..e33cfeb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)` diff --git a/README.md b/README.md index f640699..43dc1d6 100644 --- a/README.md +++ b/README.md @@ -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. |
diff --git a/lib/src/controller/card_swiper_controller.dart b/lib/src/controller/card_swiper_controller.dart index beba297..4aaac7f 100644 --- a/lib/src/controller/card_swiper_controller.dart +++ b/lib/src/controller/card_swiper_controller.dart @@ -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 dispose() async { - await _eventController.close(); + await _eventController.close(); } } diff --git a/lib/src/controller/controller_event.dart b/lib/src/controller/controller_event.dart index f5ad5d6..53f7a64 100644 --- a/lib/src/controller/controller_event.dart +++ b/lib/src/controller/controller_event.dart @@ -1,15 +1,19 @@ import 'package:flutter_card_swiper/flutter_card_swiper.dart'; -abstract class ControllerEvent { +sealed class ControllerEvent { const ControllerEvent(); } class ControllerSwipeEvent extends ControllerEvent { final CardSwiperDirection direction; - const ControllerSwipeEvent(this.direction); } class ControllerUndoEvent extends ControllerEvent { const ControllerUndoEvent(); } + +class ControllerMoveEvent extends ControllerEvent { + final int index; + const ControllerMoveEvent(this.index); +} diff --git a/lib/src/widget/card_swiper_state.dart b/lib/src/widget/card_swiper_state.dart index 0b67448..4f9ad89 100644 --- a/lib/src/widget/card_swiper_state.dart +++ b/lib/src/widget/card_swiper_state.dart @@ -161,7 +161,7 @@ class _CardSwiperState extends State return switch (event) { ControllerSwipeEvent(:final direction) => _swipe(direction), ControllerUndoEvent() => _undo(), - _ => null + ControllerMoveEvent(:final index) => _moveTo(index), }; } @@ -281,6 +281,15 @@ class _CardSwiperState extends State _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; diff --git a/test/card_swiper_controller_test.dart b/test/controller/card_swiper_controller_test.dart similarity index 87% rename from test/card_swiper_controller_test.dart rename to test/controller/card_swiper_controller_test.dart index 958876a..15f9678 100644 --- a/test/card_swiper_controller_test.dart +++ b/test/controller/card_swiper_controller_test.dart @@ -2,9 +2,9 @@ import 'package:flutter_card_swiper/flutter_card_swiper.dart'; import 'package:flutter_card_swiper/src/controller/controller_event.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'test_helpers/card_builder.dart'; -import 'test_helpers/finders.dart'; -import 'test_helpers/pump_app.dart'; +import '../test_helpers/card_builder.dart'; +import '../test_helpers/finders.dart'; +import '../test_helpers/pump_app.dart'; void main() { group('CardSwiperController', () { @@ -34,6 +34,21 @@ void main() { controller.undo(); }); + test('Swipe event adds ControllerSwipeEvent to the stream', () { + final controller = CardSwiperController(); + const index = 42; + + expectLater( + controller.events, + emits( + isA() + .having((event) => event.index, 'index', index), + ), + ); + + controller.moveTo(index); + }); + test('Dispose closes the stream', () { final controller = CardSwiperController();