Skip to content

Commit b617e09

Browse files
author
JohnE
committed
feat: support for tapping a card
1 parent 68ae4fd commit b617e09

6 files changed

+37
-1
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## [7.1.0]
2+
3+
- Adds support for tapping a card
4+
- `onTap` callback containing currentIndex.
5+
16
## [7.0.2]
27

38
- Added `CardAnimation.animateToAngle` helper to animate swipe the card to any given angle between 0-360°.

lib/src/typedefs.dart

+2
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ typedef CardSwiperOnUndo = bool Function(
3535
int currentIndex,
3636
CardSwiperDirection direction,
3737
);
38+
39+
typedef CardSwiperOnTap = FutureOr<void> Function(int currentIndex);

lib/src/widget/card_swiper.dart

+4
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ class CardSwiper extends StatefulWidget {
8888
/// Callback function that is called when the swiper is disabled.
8989
final CardSwiperOnTapDisabled? onTapDisabled;
9090

91+
/// Callback function that is called when the card is tapped (will not trigger from swipe movement)
92+
final CardSwiperOnTap? onTap;
93+
9194
/// Defined the directions in which the card is allowed to be swiped.
9295
/// Defaults to [AllowedSwipeDirection.all]
9396
final AllowedSwipeDirection allowedSwipeDirection;
@@ -134,6 +137,7 @@ class CardSwiper extends StatefulWidget {
134137
this.scale = 0.9,
135138
this.isDisabled = false,
136139
this.onTapDisabled,
140+
this.onTap,
137141
this.onSwipe,
138142
this.onEnd,
139143
this.onSwipeDirectionChange,

lib/src/widget/card_swiper_state.dart

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper>
116116
if (widget.isDisabled) {
117117
await widget.onTapDisabled?.call();
118118
}
119+
await widget.onTap?.call(_currentIndex!);
119120
},
120121
onPanStart: (tapInfo) {
121122
if (!widget.isDisabled) {

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: flutter_card_swiper
22
description: This is a Tinder-like card swiper package. It allows you to swipe left, right, up, and down and define your own business logic for each direction.
33
homepage: https://github.com/ricardodalarme/flutter_card_swiper
44
issue_tracker: https://github.com/ricardodalarme/flutter_card_swiper/issues
5-
version: 7.0.2
5+
version: 7.1.0
66

77
environment:
88
sdk: ">=3.0.0 <4.0.0"

test/card_swiper_test.dart

+24
Original file line numberDiff line numberDiff line change
@@ -634,5 +634,29 @@ void main() {
634634

635635
expect(find.card(0), findsOneWidget);
636636
});
637+
638+
testWidgets('when tapping a card, expect callback', (WidgetTester tester) async {
639+
final swiperKey = GlobalKey();
640+
var index = -1;
641+
642+
await tester.pumpApp(
643+
CardSwiper(
644+
key: swiperKey,
645+
cardsCount: 3,
646+
numberOfCardsDisplayed: 1,
647+
initialIndex: 2,
648+
onTap: (currentIndex) {
649+
index = currentIndex;
650+
},
651+
cardBuilder: genericBuilder,
652+
),
653+
);
654+
655+
await tester.tap(find.byKey(swiperKey));
656+
await tester.pumpAndSettle();
657+
658+
expect(index, 2);
659+
});
660+
637661
});
638662
}

0 commit comments

Comments
 (0)