diff --git a/lib/src/typedefs.dart b/lib/src/typedefs.dart index d7c19d2..9ef4b04 100644 --- a/lib/src/typedefs.dart +++ b/lib/src/typedefs.dart @@ -35,3 +35,5 @@ typedef CardSwiperOnUndo = bool Function( int currentIndex, CardSwiperDirection direction, ); + +typedef CardSwiperOnTap = FutureOr Function(); diff --git a/lib/src/widget/card_swiper.dart b/lib/src/widget/card_swiper.dart index 094e246..84b13fc 100644 --- a/lib/src/widget/card_swiper.dart +++ b/lib/src/widget/card_swiper.dart @@ -88,6 +88,9 @@ class CardSwiper extends StatefulWidget { /// Callback function that is called when the swiper is disabled. final CardSwiperOnTapDisabled? onTapDisabled; + /// Callback function that is called when the card is tapped (will not trigger from swipe movement) + final CardSwiperOnTap? onTap; + /// Defined the directions in which the card is allowed to be swiped. /// Defaults to [AllowedSwipeDirection.all] final AllowedSwipeDirection allowedSwipeDirection; @@ -134,6 +137,7 @@ class CardSwiper extends StatefulWidget { this.scale = 0.9, this.isDisabled = false, this.onTapDisabled, + this.onTap, this.onSwipe, this.onEnd, this.onSwipeDirectionChange, diff --git a/lib/src/widget/card_swiper_state.dart b/lib/src/widget/card_swiper_state.dart index 6a38caf..f9d413a 100644 --- a/lib/src/widget/card_swiper_state.dart +++ b/lib/src/widget/card_swiper_state.dart @@ -116,6 +116,7 @@ class _CardSwiperState extends State if (widget.isDisabled) { await widget.onTapDisabled?.call(); } + await widget.onTap?.call(); }, onPanStart: (tapInfo) { if (!widget.isDisabled) { diff --git a/test/card_swiper_test.dart b/test/card_swiper_test.dart index bf89d3f..c2c9a4c 100644 --- a/test/card_swiper_test.dart +++ b/test/card_swiper_test.dart @@ -634,5 +634,28 @@ void main() { expect(find.card(0), findsOneWidget); }); + + testWidgets('when tapping a card, expect callback', (WidgetTester tester) async { + final swiperKey = GlobalKey(); + var isCalled = false; + + await tester.pumpApp( + CardSwiper( + key: swiperKey, + cardsCount: 3, + numberOfCardsDisplayed: 1, + onTap: () { + isCalled = true; + }, + cardBuilder: genericBuilder, + ), + ); + + await tester.dragDown(swiperKey); + await tester.pumpAndSettle(); + + expect(isCalled, true); + }); + }); }