fix: swiping through the `controller` when `isDisable` is `true` (#15)

- When isDisable was true it wasn't possible to swipe using the controller
This commit is contained in:
kzrnm 2023-04-18 10:33:04 +09:00 committed by GitHub
parent 75d8ebe903
commit 095d46f21a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 273 additions and 252 deletions

View File

@ -1,3 +1,7 @@
## [4.1.3]
- Fix Swiping when `isDisabled` is `true` and triggered by the `controller`.
## [4.1.2]
- Fixes the `isHorizontalSwipingEnabled` and `isVerticalSwipingEnabled`.

View File

@ -391,7 +391,7 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper>
}
void _swipe(CardSwiperDirection direction) {
if (!_canSwipe) return;
if (_currentIndex == null) return;
_swipeType = SwipeType.swipe;
_detectedDirection = direction;

View File

@ -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: 4.1.2
version: 4.1.3
environment:
sdk: ">=2.12.0 <3.0.0"

View File

@ -43,281 +43,298 @@ void main() {
expect(controller.state, CardSwiperState.undo);
});
testWidgets('swipe() should swipe the card to the defined direction',
(tester) async {
final controller = CardSwiperController();
var direction = CardSwiperDirection.none;
for (final isDisabled in [false, true]) {
group('isDisabled=$isDisabled', () {
testWidgets('swipe() should swipe the card to the defined direction',
(tester) async {
final controller = CardSwiperController();
var direction = CardSwiperDirection.none;
await tester.pumpApp(
CardSwiper(
controller: controller,
cardsCount: 10,
cardBuilder: genericBuilder,
direction: CardSwiperDirection.top,
onSwipe: (oldIndex, currentIndex, swipeDirection) {
direction = swipeDirection;
return true;
},
),
);
await tester.pumpApp(
CardSwiper(
isDisabled: isDisabled,
controller: controller,
cardsCount: 10,
cardBuilder: genericBuilder,
direction: CardSwiperDirection.top,
onSwipe: (oldIndex, currentIndex, swipeDirection) {
direction = swipeDirection;
return true;
},
),
);
controller.swipe();
await tester.pumpAndSettle();
controller.swipe();
await tester.pumpAndSettle();
expect(direction, CardSwiperDirection.top);
});
expect(direction, CardSwiperDirection.top);
});
testWidgets('swipeLeft() should swipe the card to the left',
(tester) async {
final controller = CardSwiperController();
var direction = CardSwiperDirection.none;
testWidgets('swipeLeft() should swipe the card to the left',
(tester) async {
final controller = CardSwiperController();
var direction = CardSwiperDirection.none;
await tester.pumpApp(
CardSwiper(
controller: controller,
cardsCount: 10,
cardBuilder: genericBuilder,
direction: CardSwiperDirection.left,
onSwipe: (oldIndex, currentIndex, swipeDirection) {
direction = swipeDirection;
return true;
},
),
);
await tester.pumpApp(
CardSwiper(
isDisabled: isDisabled,
controller: controller,
cardsCount: 10,
cardBuilder: genericBuilder,
direction: CardSwiperDirection.left,
onSwipe: (oldIndex, currentIndex, swipeDirection) {
direction = swipeDirection;
return true;
},
),
);
controller.swipeLeft();
await tester.pumpAndSettle();
controller.swipeLeft();
await tester.pumpAndSettle();
expect(direction, CardSwiperDirection.left);
});
expect(direction, CardSwiperDirection.left);
});
testWidgets('swipeRight() should swipe the card to the right',
(tester) async {
final controller = CardSwiperController();
var direction = CardSwiperDirection.none;
testWidgets('swipeRight() should swipe the card to the right',
(tester) async {
final controller = CardSwiperController();
var direction = CardSwiperDirection.none;
await tester.pumpApp(
CardSwiper(
controller: controller,
cardsCount: 10,
cardBuilder: genericBuilder,
onSwipe: (oldIndex, currentIndex, swipeDirection) {
direction = swipeDirection;
return true;
},
),
);
await tester.pumpApp(
CardSwiper(
isDisabled: isDisabled,
controller: controller,
cardsCount: 10,
cardBuilder: genericBuilder,
onSwipe: (oldIndex, currentIndex, swipeDirection) {
direction = swipeDirection;
return true;
},
),
);
controller.swipeRight();
await tester.pumpAndSettle();
controller.swipeRight();
await tester.pumpAndSettle();
expect(direction, CardSwiperDirection.right);
});
expect(direction, CardSwiperDirection.right);
});
testWidgets('swipeTop() should swipe the card to the top', (tester) async {
final controller = CardSwiperController();
var direction = CardSwiperDirection.none;
testWidgets('swipeTop() should swipe the card to the top',
(tester) async {
final controller = CardSwiperController();
var direction = CardSwiperDirection.none;
await tester.pumpApp(
CardSwiper(
controller: controller,
cardsCount: 10,
cardBuilder: genericBuilder,
direction: CardSwiperDirection.top,
onSwipe: (oldIndex, currentIndex, swipeDirection) {
direction = swipeDirection;
return true;
},
),
);
await tester.pumpApp(
CardSwiper(
isDisabled: isDisabled,
controller: controller,
cardsCount: 10,
cardBuilder: genericBuilder,
direction: CardSwiperDirection.top,
onSwipe: (oldIndex, currentIndex, swipeDirection) {
direction = swipeDirection;
return true;
},
),
);
controller.swipeTop();
await tester.pumpAndSettle();
controller.swipeTop();
await tester.pumpAndSettle();
expect(direction, CardSwiperDirection.top);
});
expect(direction, CardSwiperDirection.top);
});
testWidgets('swipeBottom() should swipe the card to the bottom',
(tester) async {
final controller = CardSwiperController();
var direction = CardSwiperDirection.none;
testWidgets('swipeBottom() should swipe the card to the bottom',
(tester) async {
final controller = CardSwiperController();
var direction = CardSwiperDirection.none;
await tester.pumpApp(
CardSwiper(
controller: controller,
cardsCount: 10,
cardBuilder: genericBuilder,
direction: CardSwiperDirection.bottom,
onSwipe: (oldIndex, currentIndex, swipeDirection) {
direction = swipeDirection;
return true;
},
),
);
await tester.pumpApp(
CardSwiper(
isDisabled: isDisabled,
controller: controller,
cardsCount: 10,
cardBuilder: genericBuilder,
direction: CardSwiperDirection.bottom,
onSwipe: (oldIndex, currentIndex, swipeDirection) {
direction = swipeDirection;
return true;
},
),
);
controller.swipeBottom();
await tester.pumpAndSettle();
controller.swipeBottom();
await tester.pumpAndSettle();
expect(direction, CardSwiperDirection.bottom);
});
expect(direction, CardSwiperDirection.bottom);
});
group('undo()', () {
testWidgets('should undo the last swipe', (tester) async {
final controller = CardSwiperController();
group('undo()', () {
testWidgets('should undo the last swipe', (tester) async {
final controller = CardSwiperController();
await tester.pumpApp(
CardSwiper(
controller: controller,
cardsCount: 10,
cardBuilder: genericBuilder,
),
);
await tester.pumpApp(
CardSwiper(
isDisabled: isDisabled,
controller: controller,
cardsCount: 10,
cardBuilder: genericBuilder,
),
);
controller.swipe();
await tester.pumpAndSettle();
controller.swipe();
await tester.pumpAndSettle();
expect(find.card(1), findsOneWidget);
expect(find.card(1), findsOneWidget);
controller.undo();
await tester.pumpAndSettle();
controller.undo();
await tester.pumpAndSettle();
expect(find.card(0), findsOneWidget);
expect(find.card(0), findsOneWidget);
});
testWidgets('should undo the last swipe left', (tester) async {
final controller = CardSwiperController();
var direction = CardSwiperDirection.none;
await tester.pumpApp(
CardSwiper(
isDisabled: isDisabled,
controller: controller,
cardsCount: 10,
cardBuilder: genericBuilder,
onUndo: (_, __, swipeDirection) {
direction = swipeDirection;
return true;
},
),
);
controller.swipeLeft();
await tester.pumpAndSettle();
expect(find.card(1), findsOneWidget);
controller.undo();
await tester.pumpAndSettle();
expect(find.card(0), findsOneWidget);
expect(direction, CardSwiperDirection.left);
});
testWidgets('should undo the last swipe right', (tester) async {
final controller = CardSwiperController();
var direction = CardSwiperDirection.none;
await tester.pumpApp(
CardSwiper(
isDisabled: isDisabled,
controller: controller,
cardsCount: 10,
cardBuilder: genericBuilder,
onUndo: (_, __, swipeDirection) {
direction = swipeDirection;
return true;
},
),
);
controller.swipeRight();
await tester.pumpAndSettle();
expect(find.card(1), findsOneWidget);
controller.undo();
await tester.pumpAndSettle();
expect(find.card(0), findsOneWidget);
expect(direction, CardSwiperDirection.right);
});
testWidgets('should undo the last swipe top', (tester) async {
final controller = CardSwiperController();
var direction = CardSwiperDirection.none;
await tester.pumpApp(
CardSwiper(
isDisabled: isDisabled,
controller: controller,
cardsCount: 10,
cardBuilder: genericBuilder,
onUndo: (_, __, swipeDirection) {
direction = swipeDirection;
return true;
},
),
);
controller.swipeTop();
await tester.pumpAndSettle();
expect(find.card(1), findsOneWidget);
controller.undo();
await tester.pumpAndSettle();
expect(find.card(0), findsOneWidget);
expect(direction, CardSwiperDirection.top);
});
testWidgets('should undo the last swipe bottom', (tester) async {
final controller = CardSwiperController();
var direction = CardSwiperDirection.none;
await tester.pumpApp(
CardSwiper(
isDisabled: isDisabled,
controller: controller,
cardsCount: 10,
cardBuilder: genericBuilder,
onUndo: (_, __, swipeDirection) {
direction = swipeDirection;
return true;
},
),
);
controller.swipeBottom();
await tester.pumpAndSettle();
expect(find.card(1), findsOneWidget);
controller.undo();
await tester.pumpAndSettle();
expect(find.card(0), findsOneWidget);
expect(direction, CardSwiperDirection.bottom);
});
testWidgets('should not undo if onUndo returns false',
(tester) async {
final controller = CardSwiperController();
await tester.pumpApp(
CardSwiper(
isDisabled: isDisabled,
controller: controller,
cardsCount: 10,
cardBuilder: genericBuilder,
onUndo: (_, __, swipeDirection) {
return false;
},
),
);
controller.swipe();
await tester.pumpAndSettle();
controller.undo();
await tester.pumpAndSettle();
expect(find.card(0), findsNothing);
});
});
});
testWidgets('should undo the last swipe left', (tester) async {
final controller = CardSwiperController();
var direction = CardSwiperDirection.none;
await tester.pumpApp(
CardSwiper(
controller: controller,
cardsCount: 10,
cardBuilder: genericBuilder,
onUndo: (_, __, swipeDirection) {
direction = swipeDirection;
return true;
},
),
);
controller.swipeLeft();
await tester.pumpAndSettle();
expect(find.card(1), findsOneWidget);
controller.undo();
await tester.pumpAndSettle();
expect(find.card(0), findsOneWidget);
expect(direction, CardSwiperDirection.left);
});
testWidgets('should undo the last swipe right', (tester) async {
final controller = CardSwiperController();
var direction = CardSwiperDirection.none;
await tester.pumpApp(
CardSwiper(
controller: controller,
cardsCount: 10,
cardBuilder: genericBuilder,
onUndo: (_, __, swipeDirection) {
direction = swipeDirection;
return true;
},
),
);
controller.swipeRight();
await tester.pumpAndSettle();
expect(find.card(1), findsOneWidget);
controller.undo();
await tester.pumpAndSettle();
expect(find.card(0), findsOneWidget);
expect(direction, CardSwiperDirection.right);
});
testWidgets('should undo the last swipe top', (tester) async {
final controller = CardSwiperController();
var direction = CardSwiperDirection.none;
await tester.pumpApp(
CardSwiper(
controller: controller,
cardsCount: 10,
cardBuilder: genericBuilder,
onUndo: (_, __, swipeDirection) {
direction = swipeDirection;
return true;
},
),
);
controller.swipeTop();
await tester.pumpAndSettle();
expect(find.card(1), findsOneWidget);
controller.undo();
await tester.pumpAndSettle();
expect(find.card(0), findsOneWidget);
expect(direction, CardSwiperDirection.top);
});
testWidgets('should undo the last swipe bottom', (tester) async {
final controller = CardSwiperController();
var direction = CardSwiperDirection.none;
await tester.pumpApp(
CardSwiper(
controller: controller,
cardsCount: 10,
cardBuilder: genericBuilder,
onUndo: (_, __, swipeDirection) {
direction = swipeDirection;
return true;
},
),
);
controller.swipeBottom();
await tester.pumpAndSettle();
expect(find.card(1), findsOneWidget);
controller.undo();
await tester.pumpAndSettle();
expect(find.card(0), findsOneWidget);
expect(direction, CardSwiperDirection.bottom);
});
testWidgets('should not undo if onUndo returns false', (tester) async {
final controller = CardSwiperController();
await tester.pumpApp(
CardSwiper(
controller: controller,
cardsCount: 10,
cardBuilder: genericBuilder,
onUndo: (_, __, swipeDirection) {
return false;
},
),
);
controller.swipe();
await tester.pumpAndSettle();
controller.undo();
await tester.pumpAndSettle();
expect(find.card(0), findsNothing);
});
});
}
});
}