From 8796bc175846e03c7e5c281c237edbcc05761a56 Mon Sep 17 00:00:00 2001 From: Brian Kayfitz Date: Sat, 27 Mar 2021 22:32:27 -0400 Subject: [PATCH 1/3] apply null safety migration --- example/async_data/lib/main.dart | 6 +- example/async_data/pubspec.yaml | 2 +- example/example/pubspec.yaml | 2 +- lib/flutter_tindercard.dart | 116 ++++++++++++++++--------------- pubspec.yaml | 2 +- 5 files changed, 65 insertions(+), 63 deletions(-) diff --git a/example/async_data/lib/main.dart b/example/async_data/lib/main.dart index 6071cdc..03430aa 100644 --- a/example/async_data/lib/main.dart +++ b/example/async_data/lib/main.dart @@ -25,7 +25,7 @@ class AsyncDataExampleHomePage extends StatefulWidget { class _AsyncDataExampleHomePageState extends State with TickerProviderStateMixin { - StreamController> _streamController; + late StreamController> _streamController; List welcomeImages = [ "assets/welcome0.png", @@ -64,7 +64,7 @@ class _AsyncDataExampleHomePageState extends State initialData: welcomeImages, builder: (BuildContext context, AsyncSnapshot> snapshot) { - print('snapshot.data.length: ${snapshot.data.length}'); + print('snapshot.data.length: ${snapshot.data!.length}'); if (snapshot.hasError) return Text('Error: ${snapshot.error}'); switch (snapshot.connectionState) { case ConnectionState.none: @@ -74,7 +74,7 @@ class _AsyncDataExampleHomePageState extends State case ConnectionState.active: return _asyncDataExample( context, - snapshot.data, + snapshot.data!, (CardSwipeOrientation orientation) { // welcomeImages[0] is the swiped card // you can send to backend service in here diff --git a/example/async_data/pubspec.yaml b/example/async_data/pubspec.yaml index 4cf7067..332cc12 100644 --- a/example/async_data/pubspec.yaml +++ b/example/async_data/pubspec.yaml @@ -6,7 +6,7 @@ publish_to: "none" version: 1.0.0+1 environment: - sdk: ">=2.7.0 <3.0.0" + sdk: '>=2.12.0 <3.0.0' dependencies: flutter: diff --git a/example/example/pubspec.yaml b/example/example/pubspec.yaml index c108c1d..47b24d6 100644 --- a/example/example/pubspec.yaml +++ b/example/example/pubspec.yaml @@ -6,7 +6,7 @@ publish_to: "none" version: 1.0.0+1 environment: - sdk: ">=2.7.0 <3.0.0" + sdk: '>=2.12.0 <3.0.0' dependencies: flutter: diff --git a/lib/flutter_tindercard.dart b/lib/flutter_tindercard.dart index 0d9787c..4bfbb12 100644 --- a/lib/flutter_tindercard.dart +++ b/lib/flutter_tindercard.dart @@ -8,9 +8,9 @@ enum TriggerDirection { none, right, left, up, down } /// A Tinder-Like Widget. class TinderSwapCard extends StatefulWidget { - final CardBuilder _cardBuilder; + final CardBuilder? _cardBuilder; - final int _totalNum; + final int? _totalNum; final int _stackNum; @@ -26,11 +26,11 @@ class TinderSwapCard extends StatefulWidget { final bool _allowVerticalMovement; - final CardSwipeCompleteCallback swipeCompleteCallback; + final CardSwipeCompleteCallback? swipeCompleteCallback; - final CardDragUpdateCallback swipeUpdateCallback; + final CardDragUpdateCallback? swipeUpdateCallback; - final CardController cardController; + final CardController? cardController; final List _cardSizes = []; @@ -48,8 +48,8 @@ class TinderSwapCard extends StatefulWidget { /// value of alignment, 0.0 means middle, so it need bigger than zero. /// and size control params; TinderSwapCard({ - @required CardBuilder cardBuilder, - @required int totalNum, + required CardBuilder? cardBuilder, + required int? totalNum, AmassOrientation orientation = AmassOrientation.bottom, int stackNum = 3, int animDuration = 800, @@ -57,10 +57,10 @@ class TinderSwapCard extends StatefulWidget { double swipeEdgeVertical = 8.0, bool swipeUp = false, bool swipeDown = false, - double maxWidth, - double maxHeight, - double minWidth, - double minHeight, + required double maxWidth, + required double maxHeight, + required double minWidth, + required double minHeight, bool allowVerticalMovement = true, this.cardController, this.swipeCompleteCallback, @@ -127,19 +127,19 @@ class TinderSwapCard extends StatefulWidget { class _TinderSwapCardState extends State with TickerProviderStateMixin { - Alignment frontCardAlign; + Alignment? frontCardAlign; - AnimationController _animationController; + late AnimationController _animationController; - int _currentFront; + int? _currentFront; - static TriggerDirection _trigger; + static TriggerDirection? _trigger; Widget _buildCard(BuildContext context, int realIndex) { if (realIndex < 0) { return Container(); } - final index = realIndex - _currentFront; + final index = realIndex - _currentFront!; if (index == widget._stackNum - 1) { return Align( @@ -152,19 +152,19 @@ class _TinderSwapCardState extends State widget._swipeUp, widget._swipeDown, ).value - : frontCardAlign, + : frontCardAlign!, child: Transform.rotate( angle: (pi / 180.0) * (_animationController.status == AnimationStatus.forward ? CardAnimation.frontCardRota( - _animationController, frontCardAlign.x) + _animationController, frontCardAlign!.x) .value - : frontCardAlign.x), + : frontCardAlign!.x), child: SizedBox.fromSize( size: widget._cardSizes[index], - child: widget._cardBuilder( + child: widget._cardBuilder!( context, - widget._totalNum - realIndex - 1, + widget._totalNum! - realIndex - 1, ), ), ), @@ -173,10 +173,10 @@ class _TinderSwapCardState extends State return Align( alignment: _animationController.status == AnimationStatus.forward && - (frontCardAlign.x > 3.0 || - frontCardAlign.x < -3.0 || - frontCardAlign.y > 3 || - frontCardAlign.y < -3) + (frontCardAlign!.x > 3.0 || + frontCardAlign!.x < -3.0 || + frontCardAlign!.y > 3 || + frontCardAlign!.y < -3) ? CardAnimation.backCardAlign( _animationController, widget._cardAligns[index], @@ -185,19 +185,19 @@ class _TinderSwapCardState extends State : widget._cardAligns[index], child: SizedBox.fromSize( size: _animationController.status == AnimationStatus.forward && - (frontCardAlign.x > 3.0 || - frontCardAlign.x < -3.0 || - frontCardAlign.y > 3 || - frontCardAlign.y < -3) + (frontCardAlign!.x > 3.0 || + frontCardAlign!.x < -3.0 || + frontCardAlign!.y > 3 || + frontCardAlign!.y < -3) ? CardAnimation.backCardSize( _animationController, widget._cardSizes[index], widget._cardSizes[index + 1], ).value : widget._cardSizes[index], - child: widget._cardBuilder( + child: widget._cardBuilder!( context, - widget._totalNum - realIndex - 1, + widget._totalNum! - realIndex - 1, ), ), ); @@ -206,7 +206,7 @@ class _TinderSwapCardState extends State List _buildCards(BuildContext context) { final cards = []; - for (var i = _currentFront; i < _currentFront + widget._stackNum; i++) { + for (var i = _currentFront!; i < _currentFront! + widget._stackNum; i++) { cards.add(_buildCard(context, i)); } @@ -216,25 +216,27 @@ class _TinderSwapCardState extends State setState(() { if (widget._allowVerticalMovement == true) { frontCardAlign = Alignment( - frontCardAlign.x + + frontCardAlign!.x + details.delta.dx * 20 / MediaQuery.of(context).size.width, - frontCardAlign.y + + frontCardAlign!.y + details.delta.dy * 30 / MediaQuery.of(context).size.height, ); } else { frontCardAlign = Alignment( - frontCardAlign.x + + frontCardAlign!.x + details.delta.dx * 20 / MediaQuery.of(context).size.width, 0, ); if (widget.swipeUpdateCallback != null) { - widget.swipeUpdateCallback(details, frontCardAlign); + widget.swipeUpdateCallback!( + details, frontCardAlign ?? Alignment.center); } } if (widget.swipeUpdateCallback != null) { - widget.swipeUpdateCallback(details, frontCardAlign); + widget.swipeUpdateCallback!( + details, frontCardAlign ?? Alignment.center); } }); }, @@ -248,7 +250,7 @@ class _TinderSwapCardState extends State void animateCards(TriggerDirection trigger) { if (_animationController.isAnimating || - _currentFront + widget._stackNum == 0) { + _currentFront! + widget._stackNum == 0) { return; } _trigger = trigger; @@ -283,7 +285,7 @@ class _TinderSwapCardState extends State } void _initState() { - _currentFront = widget._totalNum - widget._stackNum; + _currentFront = widget._totalNum! - widget._stackNum; frontCardAlign = widget._cardAligns[widget._cardAligns.length - 1]; @@ -298,18 +300,18 @@ class _TinderSwapCardState extends State _animationController.addStatusListener( (final status) { - final index = widget._totalNum - widget._stackNum - _currentFront; + final index = widget._totalNum! - widget._stackNum - _currentFront!; if (status == AnimationStatus.completed) { CardSwipeOrientation orientation; - if (frontCardAlign.x < -widget._swipeEdge) { + if (frontCardAlign!.x < -widget._swipeEdge) { orientation = CardSwipeOrientation.left; - } else if (frontCardAlign.x > widget._swipeEdge) { + } else if (frontCardAlign!.x > widget._swipeEdge) { orientation = CardSwipeOrientation.right; - } else if (frontCardAlign.y < -widget._swipeEdgeVertical) { + } else if (frontCardAlign!.y < -widget._swipeEdgeVertical) { orientation = CardSwipeOrientation.up; - } else if (frontCardAlign.y > widget._swipeEdgeVertical) { + } else if (frontCardAlign!.y > widget._swipeEdgeVertical) { orientation = CardSwipeOrientation.down; } else { frontCardAlign = widget._cardAligns[widget._stackNum - 1]; @@ -317,7 +319,7 @@ class _TinderSwapCardState extends State } if (widget.swipeCompleteCallback != null) { - widget.swipeCompleteCallback(orientation, index); + widget.swipeCompleteCallback!(orientation, index); } if (orientation != CardSwipeOrientation.recover) changeCardOrder(); @@ -335,7 +337,7 @@ class _TinderSwapCardState extends State void changeCardOrder() { setState(() { - _currentFront--; + _currentFront = (_currentFront ?? 1) - 1; frontCardAlign = widget._cardAligns[widget._stackNum - 1]; }); } @@ -359,7 +361,7 @@ enum AmassOrientation { top, bottom, left, right } class CardAnimation { static Animation frontCardAlign( AnimationController controller, - Alignment beginAlign, + Alignment? beginAlign, Alignment baseAlign, double swipeEdge, bool swipeUp, @@ -368,7 +370,7 @@ class CardAnimation { double endX, endY; if (_TinderSwapCardState._trigger == TriggerDirection.none) { - endX = beginAlign.x > 0 + endX = beginAlign!.x > 0 ? (beginAlign.x > swipeEdge ? beginAlign.x + 10.0 : baseAlign.x) : (beginAlign.x < -swipeEdge ? beginAlign.x - 10.0 : baseAlign.x); endY = beginAlign.x > 3.0 || beginAlign.x < -swipeEdge @@ -388,7 +390,7 @@ class CardAnimation { } } } else if (_TinderSwapCardState._trigger == TriggerDirection.left) { - endX = beginAlign.x - swipeEdge; + endX = beginAlign!.x - swipeEdge; endY = beginAlign.y + 0.5; } /* Trigger Swipe Up or Down */ @@ -399,11 +401,11 @@ class CardAnimation { endY = beginY < -swipeEdge ? beginY - 10.0 : baseAlign.y; - endX = beginAlign.x > 0 + endX = beginAlign!.x > 0 ? (beginAlign.x > swipeEdge ? beginAlign.x + 10.0 : baseAlign.x) : (beginAlign.x < -swipeEdge ? beginAlign.x - 10.0 : baseAlign.x); } else { - endX = beginAlign.x + swipeEdge; + endX = beginAlign!.x + swipeEdge; endY = beginAlign.y + 0.5; } return AlignmentTween( @@ -427,7 +429,7 @@ class CardAnimation { ); } - static Animation backCardSize( + static Animation backCardSize( AnimationController controller, Size beginSize, Size endSize, @@ -457,29 +459,29 @@ class CardAnimation { typedef TriggerListener = void Function(TriggerDirection trigger); class CardController { - TriggerListener _listener; + TriggerListener? _listener; void triggerLeft() { if (_listener != null) { - _listener(TriggerDirection.left); + _listener!(TriggerDirection.left); } } void triggerRight() { if (_listener != null) { - _listener(TriggerDirection.right); + _listener!(TriggerDirection.right); } } void triggerUp() { if (_listener != null) { - _listener(TriggerDirection.up); + _listener!(TriggerDirection.up); } } void triggerDown() { if (_listener != null) { - _listener(TriggerDirection.down); + _listener!(TriggerDirection.down); } } diff --git a/pubspec.yaml b/pubspec.yaml index 1450357..28aa310 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -5,7 +5,7 @@ author: ShaunRain homepage: https://github.com/ShaunRain/flutter_tindercard environment: - sdk: ">=2.0.0 <3.0.0" + sdk: '>=2.12.0 <3.0.0' dependencies: flutter: From 79ec5ccfeedfa517e886f578ea9f293e312cf7df Mon Sep 17 00:00:00 2001 From: Brian Kayfitz Date: Sat, 27 Mar 2021 22:42:22 -0400 Subject: [PATCH 2/3] updating version --- pubspec.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pubspec.yaml b/pubspec.yaml index 28aa310..56c6c74 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,11 +1,11 @@ name: flutter_tindercard description: Tinder(TanTan) Card Widget, fast way to have a high quality swap card widget in your flutter app. -version: 0.2.0 +version: 0.3.0 author: ShaunRain homepage: https://github.com/ShaunRain/flutter_tindercard environment: - sdk: '>=2.12.0 <3.0.0' + sdk: ">=2.12.0 <3.0.0" dependencies: flutter: From 3edb7b2aab41f58fc32dd0d8f889d65f0d4e3ebd Mon Sep 17 00:00:00 2001 From: Brian Kayfitz Date: Tue, 20 Apr 2021 22:16:21 -0400 Subject: [PATCH 3/3] Allowing swiping to be toggle-able --- lib/flutter_tindercard.dart | 62 ++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/lib/flutter_tindercard.dart b/lib/flutter_tindercard.dart index 4bfbb12..97e7c74 100644 --- a/lib/flutter_tindercard.dart +++ b/lib/flutter_tindercard.dart @@ -26,6 +26,8 @@ class TinderSwapCard extends StatefulWidget { final bool _allowVerticalMovement; + final bool _allowSwiping; + final CardSwipeCompleteCallback? swipeCompleteCallback; final CardDragUpdateCallback? swipeUpdateCallback; @@ -57,6 +59,7 @@ class TinderSwapCard extends StatefulWidget { double swipeEdgeVertical = 8.0, bool swipeUp = false, bool swipeDown = false, + bool allowSwiping = true, required double maxWidth, required double maxHeight, required double minWidth, @@ -69,6 +72,7 @@ class TinderSwapCard extends StatefulWidget { assert(swipeEdge > 0), assert(swipeEdgeVertical > 0), assert(maxWidth > minWidth && maxHeight > minHeight), + _allowSwiping = allowSwiping, _cardBuilder = cardBuilder, _totalNum = totalNum, _stackNum = stackNum, @@ -212,34 +216,7 @@ class _TinderSwapCardState extends State cards.add(SizedBox.expand( child: GestureDetector( - onPanUpdate: (final details) { - setState(() { - if (widget._allowVerticalMovement == true) { - frontCardAlign = Alignment( - frontCardAlign!.x + - details.delta.dx * 20 / MediaQuery.of(context).size.width, - frontCardAlign!.y + - details.delta.dy * 30 / MediaQuery.of(context).size.height, - ); - } else { - frontCardAlign = Alignment( - frontCardAlign!.x + - details.delta.dx * 20 / MediaQuery.of(context).size.width, - 0, - ); - - if (widget.swipeUpdateCallback != null) { - widget.swipeUpdateCallback!( - details, frontCardAlign ?? Alignment.center); - } - } - - if (widget.swipeUpdateCallback != null) { - widget.swipeUpdateCallback!( - details, frontCardAlign ?? Alignment.center); - } - }); - }, + onPanUpdate: widget._allowSwiping ? _handlePanGesture : null, onPanEnd: (final details) { animateCards(TriggerDirection.none); }, @@ -248,6 +225,35 @@ class _TinderSwapCardState extends State return cards; } + void _handlePanGesture(DragUpdateDetails details) { + setState(() { + if (widget._allowVerticalMovement == true) { + frontCardAlign = Alignment( + frontCardAlign!.x + + details.delta.dx * 20 / MediaQuery.of(context).size.width, + frontCardAlign!.y + + details.delta.dy * 30 / MediaQuery.of(context).size.height, + ); + } else { + frontCardAlign = Alignment( + frontCardAlign!.x + + details.delta.dx * 20 / MediaQuery.of(context).size.width, + 0, + ); + + if (widget.swipeUpdateCallback != null) { + widget.swipeUpdateCallback!( + details, frontCardAlign ?? Alignment.center); + } + } + + if (widget.swipeUpdateCallback != null) { + widget.swipeUpdateCallback!( + details, frontCardAlign ?? Alignment.center); + } + }); + } + void animateCards(TriggerDirection trigger) { if (_animationController.isAnimating || _currentFront! + widget._stackNum == 0) {