Skip to content

Commit

Permalink
cancel controller subscription on dispose
Browse files Browse the repository at this point in the history
  • Loading branch information
ltOgt committed Aug 17, 2024
1 parent 8302627 commit 83d0ec6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
1 change: 1 addition & 0 deletions lib/src/widget/card_swiper.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:collection';
import 'dart:math' as math;

Expand Down
23 changes: 9 additions & 14 deletions lib/src/widget/card_swiper_state.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
part of 'card_swiper.dart';

class _CardSwiperState<T extends Widget> extends State<CardSwiper>
with SingleTickerProviderStateMixin {
class _CardSwiperState<T extends Widget> extends State<CardSwiper> with SingleTickerProviderStateMixin {
late CardAnimation _cardAnimation;
late AnimationController _animationController;

Expand All @@ -20,13 +19,15 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper>

bool get _canSwipe => _currentIndex != null && !widget.isDisabled;

StreamSubscription<ControllerEvent>? 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,
Expand Down Expand Up @@ -58,13 +59,13 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper>
_detectedVerticalDirection = direction;
}

widget.onSwipeDirectionChange
?.call(_detectedHorizontalDirection, _detectedVerticalDirection);
widget.onSwipeDirectionChange?.call(_detectedHorizontalDirection, _detectedVerticalDirection);
}

@override
void dispose() {
_animationController.dispose();
controllerSubscription?.cancel();
super.dispose();
}

Expand Down Expand Up @@ -185,9 +186,7 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper>

Future<void> _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;
Expand Down Expand Up @@ -224,14 +223,10 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper>

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;
}
Expand Down

0 comments on commit 83d0ec6

Please sign in to comment.