Skip to content

Commit

Permalink
feat(controller): add method to jump to index without animation (rica…
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardodalarme committed Jan 28, 2024
1 parent a79449d commit 1b3e905
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 7 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();
}
}
8 changes: 6 additions & 2 deletions lib/src/controller/controller_event.dart
Original file line number Diff line number Diff line change
@@ -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);
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -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', () {
Expand Down Expand Up @@ -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<ControllerMoveEvent>()
.having((event) => event.index, 'index', index),
),
);

controller.moveTo(index);
});

test('Dispose closes the stream', () {
final controller = CardSwiperController();

Expand Down

0 comments on commit 1b3e905

Please sign in to comment.