From d63bcd5c767212d4a0bedc44b8dba00910fe0a4f Mon Sep 17 00:00:00 2001 From: JohnE Date: Tue, 12 Nov 2024 20:13:16 -0800 Subject: [PATCH] new: controller command of newStack(int length) to allow for expansion and deletion of card stack data --- .../controller/card_swiper_controller.dart | 24 +++++++++++- lib/src/controller/controller_event.dart | 5 +++ lib/src/widget/card_swiper_state.dart | 37 +++++++++++++++---- 3 files changed, 57 insertions(+), 9 deletions(-) diff --git a/lib/src/controller/card_swiper_controller.dart b/lib/src/controller/card_swiper_controller.dart index 04f5619..99ddc1f 100644 --- a/lib/src/controller/card_swiper_controller.dart +++ b/lib/src/controller/card_swiper_controller.dart @@ -15,16 +15,36 @@ class CardSwiperController { _eventController.add(ControllerSwipeEvent(direction)); } - // Undo the last swipe + /// Undo the last swipe void undo() { _eventController.add(const ControllerUndoEvent()); } - // Change the top card to a specific index. + /// Change the top card to a specific index. void moveTo(int index) { _eventController.add(ControllerMoveEvent(index)); } + /// We have a new list of card data for this state machine to handle + /// 1. Set the length of the card list + /// 2. Move index to 0 (restart stack to first card) + void newStack(int length) { + _eventController.add(ControllerNewStackEvent(length)); + } + + /// We have a new added cards to the end + /// 0. Keep internal index the same (undo will work, can go backwards) + /// 1. Set the internal length of the card list + /// 2. Check if we need to redraw, if we are at end of card list + // void appendStack(int length) { + // _eventController.add(ControllerNewStackEvent(length)); + // } + + /// Set the length only, user can moveTo(0), or other actions + // void modifiedStack(int length) { + // _eventController.add(ControllerNewStackEvent(length)); + // } + Future dispose() async { await _eventController.close(); } diff --git a/lib/src/controller/controller_event.dart b/lib/src/controller/controller_event.dart index 53f7a64..a9b1f46 100644 --- a/lib/src/controller/controller_event.dart +++ b/lib/src/controller/controller_event.dart @@ -17,3 +17,8 @@ class ControllerMoveEvent extends ControllerEvent { final int index; const ControllerMoveEvent(this.index); } + +class ControllerNewStackEvent extends ControllerEvent { + final int length; + const ControllerNewStackEvent(this.length); +} diff --git a/lib/src/widget/card_swiper_state.dart b/lib/src/widget/card_swiper_state.dart index e6fa395..b6b1f93 100644 --- a/lib/src/widget/card_swiper_state.dart +++ b/lib/src/widget/card_swiper_state.dart @@ -22,10 +22,20 @@ class _CardSwiperState extends State StreamSubscription? controllerSubscription; + late NullableCardBuilder _cardBuilder; + late int _cardsCount; + @override void initState() { super.initState(); + debugPrint('[WTF] _CardSwiperState.initState !!!'); + debugPrint('[WTF] _CardSwiperState.initState !!!'); + debugPrint('[WTF] _CardSwiperState.initState !!!'); + + _cardBuilder = widget.cardBuilder; + _cardsCount = widget.cardsCount; + _undoableIndex.state = widget.initialIndex; controllerSubscription = @@ -67,6 +77,11 @@ class _CardSwiperState extends State @override void dispose() { + + debugPrint('[WTF] _CardSwiperState.dispose !!!'); + debugPrint('[WTF] _CardSwiperState.dispose !!!'); + debugPrint('[WTF] _CardSwiperState.dispose !!!'); + _animationController.dispose(); controllerSubscription?.cancel(); super.dispose(); @@ -104,7 +119,7 @@ class _CardSwiperState extends State angle: _cardAnimation.angle, child: ConstrainedBox( constraints: constraints, - child: widget.cardBuilder( + child: _cardBuilder( context, _currentIndex!, (100 * _cardAnimation.left / widget.threshold).ceil(), @@ -155,7 +170,7 @@ class _CardSwiperState extends State scale: _cardAnimation.scale - ((1 - widget.scale) * (index - 1)), child: ConstrainedBox( constraints: constraints, - child: widget.cardBuilder(context, getValidIndexOffset(index)!, 0, 0), + child: _cardBuilder(context, getValidIndexOffset(index)!, 0, 0), ), ), ); @@ -166,6 +181,8 @@ class _CardSwiperState extends State ControllerSwipeEvent(:final direction) => _swipe(direction), ControllerUndoEvent() => _undo(), ControllerMoveEvent(:final index) => _moveTo(index), + // controller + ControllerNewStackEvent(:final length) => _newCardStack(length) }; } @@ -189,7 +206,7 @@ class _CardSwiperState extends State } Future _handleCompleteSwipe() async { - final isLastCard = _currentIndex! == widget.cardsCount - 1; + final isLastCard = _currentIndex! == _cardsCount - 1; final shouldCancelSwipe = await widget.onSwipe ?.call(_currentIndex!, _nextIndex, _detectedDirection) == false; @@ -286,14 +303,20 @@ class _CardSwiperState extends State } void _moveTo(int index) { + debugPrint('[WTF] _moveTo() index: $index currentIndex: $_currentIndex cardsCount: $_cardsCount'); if (index == _currentIndex) return; - if (index < 0 || index >= widget.cardsCount) return; + if (index < 0 || index >= _cardsCount) return; setState(() { _undoableIndex.state = index; }); } + void _newCardStack(int length) { + _cardsCount = length; + _moveTo(0); // move to first card, and force redraw if necessary + } + int numberOfCardsOnScreen() { if (widget.isLoop) { return widget.numberOfCardsDisplayed; @@ -304,7 +327,7 @@ class _CardSwiperState extends State return math.min( widget.numberOfCardsDisplayed, - widget.cardsCount - _currentIndex!, + _cardsCount - _currentIndex!, ); } @@ -314,9 +337,9 @@ class _CardSwiperState extends State } final index = _currentIndex! + offset; - if (!widget.isLoop && !index.isBetween(0, widget.cardsCount - 1)) { + if (!widget.isLoop && !index.isBetween(0, _cardsCount - 1)) { return null; } - return index % widget.cardsCount; + return index % _cardsCount; } }