Skip to content

Commit

Permalink
feat: update CardDragUpdateCallback && add swipeEdge
Browse files Browse the repository at this point in the history
  • Loading branch information
长月 authored and 长月 committed Aug 1, 2019
1 parent f62a9c8 commit 5c92a4a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 21 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@

## 0.1.7
- add `animDuration` param to custom.
- Optimize the animation.
- Optimize the animation.

## 0.1.8
- update `CardDragUpdateCallback` for the swiping card's detail.
- add `swipeEdge` to determine action(recover or swipe) when you release your swiping card.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ Tinder/TanTan Card Widget.

5. Use `CardController` to trigger swap from outside. Init a CardController as param for widget, and invoke method `triggerLeft/Right` of your `CardController` to trigger swipe!

6. Use `CardDragUpdateCallback` to get swiping card's detail.

## Example
[See Here](./example/example.dart)
16 changes: 12 additions & 4 deletions example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class _ExampleHomePageState extends State<ExampleHomePage>

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

return new Scaffold(
body: new Center(
Expand All @@ -45,6 +45,7 @@ class _ExampleHomePageState extends State<ExampleHomePage>
orientation: AmassOrientation.BOTTOM,
totalNum: 6,
stackNum: 3,
swipeEdge: 4.0,
maxWidth: MediaQuery.of(context).size.width * 0.9,
maxHeight: MediaQuery.of(context).size.width * 0.9,
minWidth: MediaQuery.of(context).size.width * 0.8,
Expand All @@ -53,10 +54,17 @@ class _ExampleHomePageState extends State<ExampleHomePage>
child: Image.asset('${welcomeImages[index]}'),
),
cardController: controller = CardController(),
swipeUpdateCallback: (DragUpdateDetails details) {
/// Get swiping card's position
swipeUpdateCallback:
(DragUpdateDetails details, Alignment align) {
/// Get swiping card's alignment
if (align.x < 0) {
//Card is LEFT swiping
} else if (align.x > 0) {
//Card is RIGHT swiping
}
},
swipeCompleteCallback: (CardSwipeOrientation orientation, int index) {
swipeCompleteCallback:
(CardSwipeOrientation orientation, int index) {
/// Get orientation & index of swiped card!
}))),
);
Expand Down
35 changes: 20 additions & 15 deletions lib/flutter_tindercard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class TinderSwapCard extends StatefulWidget {
int _totalNum;
int _stackNum;
int _animDuration;
double _swipeEdge;
CardSwipeCompleteCallback swipeCompleteCallback;
CardDragUpdateCallback swipeUpdateCallback;
CardController cardController;
Expand All @@ -26,12 +27,15 @@ class TinderSwapCard extends StatefulWidget {

/// Constructor requires Card Widget Builder [cardBuilder] & your card count [totalNum]
/// , option includes: stack orientation [orientation], number of card display in same time [stackNum]
/// , [swipeEdge] is the edge to determine action(recover or swipe) when you release your swiping card
/// it is the value of alignment, 0.0 means middle, so it need bigger than zero.
/// , and size control params;
TinderSwapCard({@required CardBuilder cardBuilder,
@required int totalNum,
AmassOrientation orientation = AmassOrientation.BOTTOM,
int stackNum = 3,
int animDuration = 800,
double swipeEdge = 3.0,
double maxWidth,
double maxHeight,
double minWidth,
Expand All @@ -44,6 +48,8 @@ class TinderSwapCard extends StatefulWidget {
assert(stackNum > 1),
this._stackNum = stackNum,
this._animDuration = animDuration,
assert(swipeEdge > 0),
this._swipeEdge = swipeEdge,
assert(maxWidth > minWidth && maxHeight > minHeight)
// this._maxWidth = maxWidth,
// this._minWidth = minWidth,
Expand Down Expand Up @@ -84,7 +90,6 @@ class TinderSwapCard extends StatefulWidget {

class _TinderSwapCardState extends State<TinderSwapCard>
with SingleTickerProviderStateMixin {
double _cardRote = 0.0;
Alignment frontCardAlign;
AnimationController _animationController;
int _currentFront;
Expand All @@ -103,7 +108,8 @@ class _TinderSwapCardState extends State<TinderSwapCard>
.frontCardAlign(
_animationController,
frontCardAlign,
_cardAligns[widget._stackNum - 1])
_cardAligns[widget._stackNum - 1],
widget._swipeEdge)
.value
: frontCardAlign,
child: Transform.rotate(
Expand Down Expand Up @@ -165,10 +171,8 @@ class _TinderSwapCardState extends State<TinderSwapCard>
.size
.height);

_cardRote = frontCardAlign.x;

if (widget.swipeUpdateCallback != null) {
widget.swipeUpdateCallback(details);
widget.swipeUpdateCallback(details, frontCardAlign);
}
});
},
Expand Down Expand Up @@ -207,9 +211,10 @@ class _TinderSwapCardState extends State<TinderSwapCard>
_animationController.addStatusListener((AnimationStatus status) {
int index = widget._totalNum - widget._stackNum - _currentFront;
if (status == AnimationStatus.completed) {
if (frontCardAlign.x < 3.0 && frontCardAlign.x > -3.0) {
if (frontCardAlign.x < widget._swipeEdge &&
frontCardAlign.x > -widget._swipeEdge) {
frontCardAlign = _cardAligns[widget._stackNum - 1];
_cardRote = 0.0;

if (widget.swipeCompleteCallback != null) {
widget.swipeCompleteCallback(CardSwipeOrientation.RECOVER, index);
}
Expand Down Expand Up @@ -238,7 +243,6 @@ class _TinderSwapCardState extends State<TinderSwapCard>
changeCardOrder() {
setState(() {
_currentFront--;
_cardRote = 0.0;
frontCardAlign = _cardAligns[widget._stackNum - 1];
});
}
Expand All @@ -254,26 +258,27 @@ typedef CardSwipeCompleteCallback = void Function(
CardSwipeOrientation orientation, int index);

/// [DragUpdateDetails] of swiping card.
typedef CardDragUpdateCallback = void Function(DragUpdateDetails details);
typedef CardDragUpdateCallback = void Function(DragUpdateDetails details, Alignment align);

enum AmassOrientation { TOP, BOTTOM, LEFT, RIGHT }

class CardAnimation {
static Animation<Alignment> frontCardAlign(AnimationController controller,
Alignment beginAlign, Alignment baseAlign) {
Alignment beginAlign, Alignment baseAlign, double swipeEdge) {
double endX, endY;

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);
? (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 < -3.0 ? beginAlign.y : baseAlign.y;
beginAlign.x > 3.0 || beginAlign.x < -swipeEdge ? beginAlign.y : baseAlign
.y;
} else if (_TinderSwapCardState._trigger == -1) {
endX = beginAlign.x - 3.0;
endX = beginAlign.x - swipeEdge;
endY = beginAlign.y + 0.5;
} else {
endX = beginAlign.x + 3.0;
endX = beginAlign.x + swipeEdge;
endY = beginAlign.y + 0.5;
}
return new AlignmentTween(
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_tindercard
description: Tinder(TanTan) Card Widget, fast way to have a high quality swap card widget in your flutter app.
version: 0.1.7
version: 0.1.8
author: ShaunRain <[email protected]>
homepage: https://github.com/ShaunRain/flutter_tindercard

Expand Down

0 comments on commit 5c92a4a

Please sign in to comment.