diff --git a/lib/src/widget/card_swiper.dart b/lib/src/widget/card_swiper.dart index 5d129ff..9eae6dd 100644 --- a/lib/src/widget/card_swiper.dart +++ b/lib/src/widget/card_swiper.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'dart:collection'; import 'dart:math' as math; diff --git a/lib/src/widget/card_swiper_state.dart b/lib/src/widget/card_swiper_state.dart index 30a5925..6873378 100644 --- a/lib/src/widget/card_swiper_state.dart +++ b/lib/src/widget/card_swiper_state.dart @@ -1,7 +1,6 @@ part of 'card_swiper.dart'; -class _CardSwiperState extends State - with SingleTickerProviderStateMixin { +class _CardSwiperState extends State with SingleTickerProviderStateMixin { late CardAnimation _cardAnimation; late AnimationController _animationController; @@ -20,13 +19,15 @@ class _CardSwiperState extends State bool get _canSwipe => _currentIndex != null && !widget.isDisabled; + StreamSubscription? controllerSubscription; + @override void initState() { super.initState(); _undoableIndex.state = widget.initialIndex; - widget.controller?.events.listen(_controllerListener); + controllerSubscription = widget.controller?.events.listen(_controllerListener); _animationController = AnimationController( duration: widget.duration, @@ -58,13 +59,13 @@ class _CardSwiperState extends State _detectedVerticalDirection = direction; } - widget.onSwipeDirectionChange - ?.call(_detectedHorizontalDirection, _detectedVerticalDirection); + widget.onSwipeDirectionChange?.call(_detectedHorizontalDirection, _detectedVerticalDirection); } @override void dispose() { _animationController.dispose(); + controllerSubscription?.cancel(); super.dispose(); } @@ -185,9 +186,7 @@ class _CardSwiperState extends State Future _handleCompleteSwipe() async { final isLastCard = _currentIndex! == widget.cardsCount - 1; - final shouldCancelSwipe = await widget.onSwipe - ?.call(_currentIndex!, _nextIndex, _detectedDirection) == - false; + final shouldCancelSwipe = await widget.onSwipe?.call(_currentIndex!, _nextIndex, _detectedDirection) == false; if (shouldCancelSwipe) { return; @@ -224,14 +223,10 @@ class _CardSwiperState extends State CardSwiperDirection _getEndAnimationDirection() { if (_cardAnimation.left.abs() > widget.threshold) { - return _cardAnimation.left.isNegative - ? CardSwiperDirection.left - : CardSwiperDirection.right; + return _cardAnimation.left.isNegative ? CardSwiperDirection.left : CardSwiperDirection.right; } if (_cardAnimation.top.abs() > widget.threshold) { - return _cardAnimation.top.isNegative - ? CardSwiperDirection.top - : CardSwiperDirection.bottom; + return _cardAnimation.top.isNegative ? CardSwiperDirection.top : CardSwiperDirection.bottom; } return CardSwiperDirection.none; }