diff --git a/CHANGELOG.md b/CHANGELOG.md index cc98711..bd37721 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## NEXT + +- **BREAKING CHANGES**: + - Add currentIndex and previousIndex to the onSwipe callback + ## [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. diff --git a/example/lib/main.dart b/example/lib/main.dart index 58189a1..473ab72 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -75,7 +75,13 @@ class _ExamplePageState extends State { ); } - void _swipe(int index, CardSwiperDirection direction) { - debugPrint('the card $index was swiped to the: ${direction.name}'); + void _swipe( + int previousIndex, + int currentIndex, + CardSwiperDirection direction, + ) { + debugPrint( + 'the card $previousIndex was swiped to the ${direction.name}. Now the card $currentIndex is on top', + ); } } diff --git a/lib/src/card_swiper.dart b/lib/src/card_swiper.dart index 6472692..ab6933a 100644 --- a/lib/src/card_swiper.dart +++ b/lib/src/card_swiper.dart @@ -316,8 +316,15 @@ class _CardSwiperState extends State> if (status == AnimationStatus.completed) { setState(() { if (_swipeType == SwipeType.swipe) { - widget.onSwipe?.call(_currentIndex, detectedDirection); + final previousIndex = _currentIndex; _stack.removeAt(_currentIndex); + widget.onSwipe?.call( + previousIndex, + widget.isLoop && _stack.isEmpty + ? widget.cards.length - 1 + : _currentIndex, + detectedDirection, + ); if (_stack.isEmpty) { widget.onEnd?.call(); diff --git a/lib/src/typedefs.dart b/lib/src/typedefs.dart index a83bf04..6426e62 100644 --- a/lib/src/typedefs.dart +++ b/lib/src/typedefs.dart @@ -1,7 +1,8 @@ import 'package:flutter_card_swiper/src/enums.dart'; typedef CardSwiperOnSwipe = void Function( - int index, + int previousIndex, + int currentIndex, CardSwiperDirection direction, );