Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support for tapping a card #48

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [7.1.0]

- Adds support for tapping a card
- `onTap` callback containing currentIndex.

## [7.0.2]

- Added `CardAnimation.animateToAngle` helper to animate swipe the card to any given angle between 0-360°.
Expand Down
2 changes: 2 additions & 0 deletions lib/src/typedefs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ typedef CardSwiperOnUndo = bool Function(
int currentIndex,
CardSwiperDirection direction,
);

typedef CardSwiperOnTap = FutureOr<void> Function(int currentIndex);
4 changes: 4 additions & 0 deletions lib/src/widget/card_swiper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions lib/src/widget/card_swiper_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper>
if (widget.isDisabled) {
await widget.onTapDisabled?.call();
}
await widget.onTap?.call(_currentIndex!);
},
onPanStart: (tapInfo) {
if (!widget.isDisabled) {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: flutter_card_swiper
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.
homepage: https://github.com/ricardodalarme/flutter_card_swiper
issue_tracker: https://github.com/ricardodalarme/flutter_card_swiper/issues
version: 7.0.2
version: 7.1.0

environment:
sdk: ">=3.0.0 <4.0.0"
Expand Down
24 changes: 24 additions & 0 deletions test/card_swiper_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -634,5 +634,29 @@ void main() {

expect(find.card(0), findsOneWidget);
});

testWidgets('when tapping a card, expect callback',
(WidgetTester tester) async {
final swiperKey = GlobalKey();
var index = -1;

await tester.pumpApp(
CardSwiper(
key: swiperKey,
cardsCount: 3,
numberOfCardsDisplayed: 1,
initialIndex: 2,
onTap: (currentIndex) {
index = currentIndex;
},
cardBuilder: genericBuilder,
),
);

await tester.tap(find.byKey(swiperKey));
await tester.pumpAndSettle();

expect(index, 2);
});
});
}