Skip to content

Commit

Permalink
refactor: improve _onEndAnimation legibility
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardodalarme committed Jan 9, 2024
1 parent 3bf329d commit bfd9185
Showing 1 changed file with 31 additions and 21 deletions.
52 changes: 31 additions & 21 deletions lib/src/card_swiper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -409,32 +409,42 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper>
}

void _onEndAnimation() {
final direction = _getEndAnimationDirection();
final isValidDirection = this._isValidDirection(direction);

if (isValidDirection) {
_swipe(direction);
} else {
_goBack();
}
}

CardSwiperDirection _getEndAnimationDirection() {
if (_cardAnimation.left.abs() > widget.threshold) {
final direction = _cardAnimation.left.isNegative
return _cardAnimation.left.isNegative
? CardSwiperDirection.left
: CardSwiperDirection.right;
if (direction == CardSwiperDirection.left &&
widget.allowedSwipeDirection.left ||
direction == CardSwiperDirection.right &&
widget.allowedSwipeDirection.right) {
_swipe(direction);
} else {
_goBack();
}
} else if (_cardAnimation.top.abs() > widget.threshold) {
final direction = _cardAnimation.top.isNegative
}
if (_cardAnimation.top.abs() > widget.threshold) {
return _cardAnimation.top.isNegative
? CardSwiperDirection.top
: CardSwiperDirection.bottom;
if (direction == CardSwiperDirection.top &&
widget.allowedSwipeDirection.up ||
direction == CardSwiperDirection.bottom &&
widget.allowedSwipeDirection.down) {
_swipe(direction);
} else {
_goBack();
}
} else {
_goBack();
}
return CardSwiperDirection.none;
}

bool _isValidDirection(CardSwiperDirection direction) {
switch (direction) {
case CardSwiperDirection.left:
return widget.allowedSwipeDirection.left;
case CardSwiperDirection.right:
return widget.allowedSwipeDirection.right;
case CardSwiperDirection.top:
return widget.allowedSwipeDirection.up;
case CardSwiperDirection.bottom:
return widget.allowedSwipeDirection.down;
default:
return false;
}
}

Expand Down

0 comments on commit bfd9185

Please sign in to comment.