feat(controller): add method to jump to index without animation (#25)
This commit is contained in:
parent
a79449dfe6
commit
1b3e905e82
|
|
@ -1,6 +1,7 @@
|
||||||
|
|
||||||
## [7.0.0]
|
## [7.0.0]
|
||||||
|
|
||||||
|
- Adds `moveTo` method to `CardSwiperController` to move to a specific card index.
|
||||||
- **BREAKING CHANGE**:
|
- **BREAKING CHANGE**:
|
||||||
- Upgrade min dart sdk to 3.0.0
|
- Upgrade min dart sdk to 3.0.0
|
||||||
- Replace `swipe`, `swipeLeft`, `swipeRight`, `swipeUp`, `swipeDown` with `swipe(CardSwiperDirection direction)`
|
- Replace `swipe`, `swipeLeft`, `swipeRight`, `swipeUp`, `swipeDown` with `swipe(CardSwiperDirection direction)`
|
||||||
|
|
|
||||||
|
|
@ -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. |
|
| swipe | Swipes the card to a specific direction. |
|
||||||
| undo | Bring back the last card that was swiped away. |
|
| undo | Bring back the last card that was swiped away. |
|
||||||
|
| moveTo | Change the top card to a specific index. |
|
||||||
|
|
||||||
<hr/>
|
<hr/>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,12 @@ class CardSwiperController {
|
||||||
_eventController.add(const ControllerUndoEvent());
|
_eventController.add(const ControllerUndoEvent());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Change the top card to a specific index.
|
||||||
|
void moveTo(int index) {
|
||||||
|
_eventController.add(ControllerMoveEvent(index));
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> dispose() async {
|
Future<void> dispose() async {
|
||||||
await _eventController.close();
|
await _eventController.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,19 @@
|
||||||
import 'package:flutter_card_swiper/flutter_card_swiper.dart';
|
import 'package:flutter_card_swiper/flutter_card_swiper.dart';
|
||||||
|
|
||||||
abstract class ControllerEvent {
|
sealed class ControllerEvent {
|
||||||
const ControllerEvent();
|
const ControllerEvent();
|
||||||
}
|
}
|
||||||
|
|
||||||
class ControllerSwipeEvent extends ControllerEvent {
|
class ControllerSwipeEvent extends ControllerEvent {
|
||||||
final CardSwiperDirection direction;
|
final CardSwiperDirection direction;
|
||||||
|
|
||||||
const ControllerSwipeEvent(this.direction);
|
const ControllerSwipeEvent(this.direction);
|
||||||
}
|
}
|
||||||
|
|
||||||
class ControllerUndoEvent extends ControllerEvent {
|
class ControllerUndoEvent extends ControllerEvent {
|
||||||
const ControllerUndoEvent();
|
const ControllerUndoEvent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class ControllerMoveEvent extends ControllerEvent {
|
||||||
|
final int index;
|
||||||
|
const ControllerMoveEvent(this.index);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -161,7 +161,7 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper>
|
||||||
return switch (event) {
|
return switch (event) {
|
||||||
ControllerSwipeEvent(:final direction) => _swipe(direction),
|
ControllerSwipeEvent(:final direction) => _swipe(direction),
|
||||||
ControllerUndoEvent() => _undo(),
|
ControllerUndoEvent() => _undo(),
|
||||||
_ => null
|
ControllerMoveEvent(:final index) => _moveTo(index),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -281,6 +281,15 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper>
|
||||||
_cardAnimation.animateUndo(context, direction);
|
_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() {
|
int numberOfCardsOnScreen() {
|
||||||
if (widget.isLoop) {
|
if (widget.isLoop) {
|
||||||
return widget.numberOfCardsDisplayed;
|
return widget.numberOfCardsDisplayed;
|
||||||
|
|
|
||||||
|
|
@ -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_card_swiper/src/controller/controller_event.dart';
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
|
||||||
import 'test_helpers/card_builder.dart';
|
import '../test_helpers/card_builder.dart';
|
||||||
import 'test_helpers/finders.dart';
|
import '../test_helpers/finders.dart';
|
||||||
import 'test_helpers/pump_app.dart';
|
import '../test_helpers/pump_app.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
group('CardSwiperController', () {
|
group('CardSwiperController', () {
|
||||||
|
|
@ -34,6 +34,21 @@ void main() {
|
||||||
controller.undo();
|
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', () {
|
test('Dispose closes the stream', () {
|
||||||
final controller = CardSwiperController();
|
final controller = CardSwiperController();
|
||||||
|
|
||||||
Loading…
Reference in New Issue