refactor: improve _onEndAnimation legibility
This commit is contained in:
parent
3bf329de34
commit
bfd9185284
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue