From 83d0ec6ea2ca5b006ccb1bb3ce6da279de5cb977 Mon Sep 17 00:00:00 2001 From: ltOgt Date: Sat, 17 Aug 2024 11:28:38 +0200 Subject: [PATCH 1/4] cancel controller subscription on dispose --- lib/src/widget/card_swiper.dart | 1 + lib/src/widget/card_swiper_state.dart | 23 +++++++++-------------- 2 files changed, 10 insertions(+), 14 deletions(-) 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; } From c9914ccef883df8b7a85294b522321876ecda6be Mon Sep 17 00:00:00 2001 From: ltOgt Date: Sat, 17 Aug 2024 11:31:03 +0200 Subject: [PATCH 2/4] update pubspec and changelog --- CHANGELOG.md | 4 ++++ pubspec.yaml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 93803fd..9746a77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## [7.0.2] + +- Tracks `StreamSubscription` given `widget.controller`, and calls `cancel` on dispose. + ## [7.0.1] - Prevents `CardSwiperController` to be disposed by `CardSwiper`. diff --git a/pubspec.yaml b/pubspec.yaml index 4fa096e..5044c54 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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.1 +version: 7.0.2 environment: sdk: ">=3.0.0 <4.0.0" From a419b4b8e0857e44860810ee051a56a6aac8e248 Mon Sep 17 00:00:00 2001 From: ltOgt Date: Sat, 17 Aug 2024 11:54:51 +0200 Subject: [PATCH 3/4] undo format changes --- lib/src/widget/card_swiper_state.dart | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/src/widget/card_swiper_state.dart b/lib/src/widget/card_swiper_state.dart index 6873378..95d852b 100644 --- a/lib/src/widget/card_swiper_state.dart +++ b/lib/src/widget/card_swiper_state.dart @@ -1,6 +1,7 @@ part of 'card_swiper.dart'; -class _CardSwiperState extends State with SingleTickerProviderStateMixin { +class _CardSwiperState extends State + with SingleTickerProviderStateMixin { late CardAnimation _cardAnimation; late AnimationController _animationController; @@ -59,7 +60,8 @@ class _CardSwiperState extends State with SingleTi _detectedVerticalDirection = direction; } - widget.onSwipeDirectionChange?.call(_detectedHorizontalDirection, _detectedVerticalDirection); + widget.onSwipeDirectionChange + ?.call(_detectedHorizontalDirection, _detectedVerticalDirection); } @override @@ -186,7 +188,8 @@ class _CardSwiperState extends State with SingleTi 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; @@ -223,10 +226,14 @@ class _CardSwiperState extends State with SingleTi 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; } From 27bd2a0ba3d34681fa1d612676cce04cff791da2 Mon Sep 17 00:00:00 2001 From: ltOgt Date: Sat, 17 Aug 2024 11:56:44 +0200 Subject: [PATCH 4/4] undo more format changes --- lib/src/widget/card_swiper_state.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/src/widget/card_swiper_state.dart b/lib/src/widget/card_swiper_state.dart index 95d852b..546d9af 100644 --- a/lib/src/widget/card_swiper_state.dart +++ b/lib/src/widget/card_swiper_state.dart @@ -189,7 +189,8 @@ class _CardSwiperState extends State Future _handleCompleteSwipe() async { final isLastCard = _currentIndex! == widget.cardsCount - 1; final shouldCancelSwipe = await widget.onSwipe - ?.call(_currentIndex!, _nextIndex, _detectedDirection) == false; + ?.call(_currentIndex!, _nextIndex, _detectedDirection) == + false; if (shouldCancelSwipe) { return;