feat(callback): add currentIndex and previoudIndex to `onSwipe` (#7)

This commit is contained in:
ricardodalarme 2023-03-18 13:25:51 -03:00
parent a8cbd3a110
commit 5edd21e371
4 changed files with 23 additions and 4 deletions

View File

@ -1,3 +1,8 @@
## NEXT
- **BREAKING CHANGES**:
- Add currentIndex and previousIndex to the onSwipe callback
## [2.1.0] ## [2.1.0]
- Add option to display more cards at a time. Useful if the widgets you want in your cards take time to build (for example a network image or video): displaying more cards builds them in advance and makes a fast serie of swipes more fluid. - Add option to display more cards at a time. Useful if the widgets you want in your cards take time to build (for example a network image or video): displaying more cards builds them in advance and makes a fast serie of swipes more fluid.

View File

@ -75,7 +75,13 @@ class _ExamplePageState extends State<Example> {
); );
} }
void _swipe(int index, CardSwiperDirection direction) { void _swipe(
debugPrint('the card $index was swiped to the: ${direction.name}'); int previousIndex,
int currentIndex,
CardSwiperDirection direction,
) {
debugPrint(
'the card $previousIndex was swiped to the ${direction.name}. Now the card $currentIndex is on top',
);
} }
} }

View File

@ -316,8 +316,15 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper<T>>
if (status == AnimationStatus.completed) { if (status == AnimationStatus.completed) {
setState(() { setState(() {
if (_swipeType == SwipeType.swipe) { if (_swipeType == SwipeType.swipe) {
widget.onSwipe?.call(_currentIndex, detectedDirection); final previousIndex = _currentIndex;
_stack.removeAt(_currentIndex); _stack.removeAt(_currentIndex);
widget.onSwipe?.call(
previousIndex,
widget.isLoop && _stack.isEmpty
? widget.cards.length - 1
: _currentIndex,
detectedDirection,
);
if (_stack.isEmpty) { if (_stack.isEmpty) {
widget.onEnd?.call(); widget.onEnd?.call();

View File

@ -1,7 +1,8 @@
import 'package:flutter_card_swiper/src/enums.dart'; import 'package:flutter_card_swiper/src/enums.dart';
typedef CardSwiperOnSwipe = void Function( typedef CardSwiperOnSwipe = void Function(
int index, int previousIndex,
int currentIndex,
CardSwiperDirection direction, CardSwiperDirection direction,
); );