From b684ffffbef32f614057b2921fdcd79b6e9dce6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=95=BF=E6=9C=88?= Date: Wed, 17 Jul 2019 15:36:55 +0800 Subject: [PATCH] feat: add CardController --- CHANGELOG.md | 5 ++- example/example.dart | 3 ++ lib/flutter_tindercard.dart | 70 ++++++++++++++++++++++++++++++++----- 3 files changed, 68 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8cad681..d9dfb8e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,4 +4,7 @@ - add `CardDragUpdateCallback` for the swiping position. ## 0.1.5 -- update `CardSwipeCompleteCallback` with the card's index param: \ No newline at end of file +- update `CardSwipeCompleteCallback` with the card's index param: + +## 0.1.6 +- add `CardController` to trigger swap from outside. \ No newline at end of file diff --git a/example/example.dart b/example/example.dart index c6480c3..569973c 100644 --- a/example/example.dart +++ b/example/example.dart @@ -35,6 +35,8 @@ class _ExampleHomePageState extends State @override Widget build(BuildContext context) { + CardController controller; //Use this to trigger swap. + return new Scaffold( body: new Center( child: Container( @@ -50,6 +52,7 @@ class _ExampleHomePageState extends State cardBuilder: (context, index) => Card( child: Image.asset('${welcomeImages[index]}'), ), + cardController: controller = CardController(), swipeUpdateCallback: (DragUpdateDetails details) { /// Get swiping card's position }, diff --git a/lib/flutter_tindercard.dart b/lib/flutter_tindercard.dart index 2781667..d12149d 100644 --- a/lib/flutter_tindercard.dart +++ b/lib/flutter_tindercard.dart @@ -13,6 +13,7 @@ class TinderSwapCard extends StatefulWidget { int _stackNum; CardSwipeCompleteCallback swipeCompleteCallback; CardDragUpdateCallback swipeUpdateCallback; + CardController cardController; // double _maxWidth; // double _minWidth; @@ -34,6 +35,7 @@ class TinderSwapCard extends StatefulWidget { double maxHeight, double minWidth, double minHeight, + this.cardController, this.swipeCompleteCallback, this.swipeUpdateCallback}) : this._cardBuilder = cardBuilder, @@ -84,6 +86,7 @@ class _TinderSwapCardState extends State Alignment frontCardAlign; AnimationController _animationController; int _currentFront; + static int _trigger; // 0: no trigger; -1: trigger left; 1: trigger right Widget _buildCard(BuildContext context, int realIndex) { if (realIndex < 0) { @@ -94,7 +97,9 @@ class _TinderSwapCardState extends State if (index == widget._stackNum - 1) { return Align( alignment: _animationController.status == AnimationStatus.forward - ? CardAnimation.frontCardAlign(_animationController, frontCardAlign, + ? frontCardAlign = CardAnimation.frontCardAlign( + _animationController, + frontCardAlign, _cardAligns[widget._stackNum - 1]) .value : frontCardAlign, @@ -156,19 +161,28 @@ class _TinderSwapCardState extends State }); }, onPanEnd: (DragEndDetails details) { - animateCards(); + animateCards(0); }, ), )); return cards; } - animateCards() { + animateCards(int trigger) { + if (_animationController.isAnimating || + _currentFront + widget._stackNum == 0) { + return; + } + _trigger = trigger; _animationController.stop(); _animationController.value = 0.0; _animationController.forward(); } + void triggerSwap(int trigger) { + animateCards(trigger); + } + @override void initState() { super.initState(); @@ -189,9 +203,11 @@ class _TinderSwapCardState extends State } } else { if (widget.swipeCompleteCallback != null) { - widget.swipeCompleteCallback(frontCardAlign.x < 0 - ? CardSwipeOrientation.LEFT - : CardSwipeOrientation.RIGHT, index); + widget.swipeCompleteCallback( + frontCardAlign.x < 0 + ? CardSwipeOrientation.LEFT + : CardSwipeOrientation.RIGHT, + index); } changeCardOrder(); @@ -202,6 +218,8 @@ class _TinderSwapCardState extends State @override Widget build(BuildContext context) { + widget.cardController?.addListener((trigger) => triggerSwap(trigger)); + return Stack(children: _buildCards(context)); } @@ -231,9 +249,17 @@ enum AmassOrientation { TOP, BOTTOM, LEFT, RIGHT } class CardAnimation { static Animation frontCardAlign(AnimationController controller, Alignment beginAlign, Alignment baseAlign) { - double endX = beginAlign.x > 0 - ? (beginAlign.x > 3.0 ? beginAlign.x + 10.0 : baseAlign.x) - : (beginAlign.x < -3.0 ? beginAlign.x - 10.0 : baseAlign.x); + double endX; + + if (_TinderSwapCardState._trigger == 0) { + endX = beginAlign.x > 0 + ? (beginAlign.x > 3.0 ? beginAlign.x + 10.0 : baseAlign.x) + : (beginAlign.x < -3.0 ? beginAlign.x - 10.0 : baseAlign.x); + } else if (_TinderSwapCardState._trigger == -1) { + endX = beginAlign.x - 5.0; + } else { + endX = beginAlign.x + 5.0; + } return new AlignmentTween( begin: beginAlign, end: new Alignment(endX, baseAlign.y)) .animate( @@ -258,3 +284,29 @@ class CardAnimation { new CurvedAnimation(parent: controller, curve: Curves.easeOut)); } } + +typedef TriggerListener = void Function(int trigger); + +class CardController { + TriggerListener _listener; + + void triggerLeft() { + if (_listener != null) { + _listener(-1); + } + } + + void triggerRight() { + if (_listener != null) { + _listener(1); + } + } + + void addListener(listener) { + _listener = listener; + } + + void removeListener() { + _listener = null; + } +}