Skip to content

Commit

Permalink
feat: add CardController
Browse files Browse the repository at this point in the history
  • Loading branch information
长月 authored and 长月 committed Jul 17, 2019
1 parent df14605 commit b684fff
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 10 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
- add `CardDragUpdateCallback` for the swiping position.

## 0.1.5
- update `CardSwipeCompleteCallback` with the card's index param:
- update `CardSwipeCompleteCallback` with the card's index param:

## 0.1.6
- add `CardController` to trigger swap from outside.
3 changes: 3 additions & 0 deletions example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class _ExampleHomePageState extends State<ExampleHomePage>

@override
Widget build(BuildContext context) {
CardController controller; //Use this to trigger swap.

return new Scaffold(
body: new Center(
child: Container(
Expand All @@ -50,6 +52,7 @@ class _ExampleHomePageState extends State<ExampleHomePage>
cardBuilder: (context, index) => Card(
child: Image.asset('${welcomeImages[index]}'),
),
cardController: controller = CardController(),
swipeUpdateCallback: (DragUpdateDetails details) {
/// Get swiping card's position
},
Expand Down
70 changes: 61 additions & 9 deletions lib/flutter_tindercard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class TinderSwapCard extends StatefulWidget {
int _stackNum;
CardSwipeCompleteCallback swipeCompleteCallback;
CardDragUpdateCallback swipeUpdateCallback;
CardController cardController;

// double _maxWidth;
// double _minWidth;
Expand All @@ -34,6 +35,7 @@ class TinderSwapCard extends StatefulWidget {
double maxHeight,
double minWidth,
double minHeight,
this.cardController,
this.swipeCompleteCallback,
this.swipeUpdateCallback})
: this._cardBuilder = cardBuilder,
Expand Down Expand Up @@ -84,6 +86,7 @@ class _TinderSwapCardState extends State<TinderSwapCard>
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) {
Expand All @@ -94,7 +97,9 @@ class _TinderSwapCardState extends State<TinderSwapCard>
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,
Expand Down Expand Up @@ -156,19 +161,28 @@ class _TinderSwapCardState extends State<TinderSwapCard>
});
},
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();
Expand All @@ -189,9 +203,11 @@ class _TinderSwapCardState extends State<TinderSwapCard>
}
} 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();
Expand All @@ -202,6 +218,8 @@ class _TinderSwapCardState extends State<TinderSwapCard>

@override
Widget build(BuildContext context) {
widget.cardController?.addListener((trigger) => triggerSwap(trigger));

return Stack(children: _buildCards(context));
}

Expand Down Expand Up @@ -231,9 +249,17 @@ enum AmassOrientation { TOP, BOTTOM, LEFT, RIGHT }
class CardAnimation {
static Animation<Alignment> 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(
Expand All @@ -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;
}
}

0 comments on commit b684fff

Please sign in to comment.