Skip to content

Commit

Permalink
feat: allow customize the backcard offset (ricardodalarme#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardodalarme authored May 1, 2023
1 parent 852480f commit b2dd0e0
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 23 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## NEXT
## [5.0.0]

- Adds option to customize the back card offset.
- **BREAKING CHANGE**:
- `CardSwiperController` is no longer disposed by `CardSwiper`, but who created it must dispose it.

Expand Down
3 changes: 2 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ class _ExamplePageState extends State<Example> {
child: CardSwiper(
controller: controller,
cardsCount: cards.length,
numberOfCardsDisplayed: 3,
onSwipe: _onSwipe,
onUndo: _onUndo,
numberOfCardsDisplayed: 3,
backCardOffset: const Offset(40, 40),
padding: const EdgeInsets.all(24.0),
cardBuilder: (context, index) => cards[index],
),
Expand Down
40 changes: 24 additions & 16 deletions lib/src/card_animation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ class CardAnimation {
required this.animationController,
required this.maxAngle,
required this.initialScale,
required this.initialOffset,
this.isHorizontalSwipingEnabled = true,
this.isVerticalSwipingEnabled = true,
}) : scale = initialScale;
}) : scale = initialScale,
difference = initialOffset;

final double maxAngle;
final double initialScale;
final Offset initialOffset;
final AnimationController animationController;
final bool isHorizontalSwipingEnabled;
final bool isVerticalSwipingEnabled;
Expand All @@ -24,12 +27,12 @@ class CardAnimation {
double total = 0;
double angle = 0;
double scale;
double difference = 40;
Offset difference;

late Animation<double> _leftAnimation;
late Animation<double> _topAnimation;
late Animation<double> _scaleAnimation;
late Animation<double> _differenceAnimation;
late Animation<Offset> _differenceAnimation;

double get _maxAngleInRadian => maxAngle * (pi / 180);

Expand All @@ -47,7 +50,7 @@ class CardAnimation {
total = 0;
angle = 0;
scale = initialScale;
difference = 40;
difference = initialOffset;
}

void update(double dx, double dy, bool inverseAngle) {
Expand Down Expand Up @@ -79,8 +82,13 @@ class CardAnimation {
}

void updateDifference() {
if (difference.isBetween(0, difference)) {
difference = (total > 0) ? 40 - (total / 10) : 40 + (total / 10);
final discrepancy = (total > 0) ? total / 10 : -(total / 10);

if (difference.dx.isBetween(0, initialOffset.dx)) {
difference = Offset(initialOffset.dx + discrepancy, difference.dy);
}
if (difference.dy.isBetween(0, initialOffset.dy)) {
difference = Offset(difference.dx, initialOffset.dy + discrepancy);
}
}

Expand Down Expand Up @@ -114,9 +122,9 @@ class CardAnimation {
begin: scale,
end: 1.0,
).animate(animationController);
_differenceAnimation = Tween<double>(
_differenceAnimation = Tween<Offset>(
begin: difference,
end: 0,
end: Offset.zero,
).animate(animationController);
animationController.forward();
}
Expand All @@ -136,9 +144,9 @@ class CardAnimation {
begin: scale,
end: 1.0,
).animate(animationController);
_differenceAnimation = Tween<double>(
_differenceAnimation = Tween<Offset>(
begin: difference,
end: 0,
end: Offset.zero,
).animate(animationController);
animationController.forward();
}
Expand All @@ -156,9 +164,9 @@ class CardAnimation {
begin: scale,
end: initialScale,
).animate(animationController);
_differenceAnimation = Tween<double>(
_differenceAnimation = Tween<Offset>(
begin: difference,
end: 40,
end: initialOffset,
).animate(animationController);
animationController.forward();
}
Expand Down Expand Up @@ -193,8 +201,8 @@ class CardAnimation {
begin: 1.0,
end: scale,
).animate(animationController);
_differenceAnimation = Tween<double>(
begin: 0,
_differenceAnimation = Tween<Offset>(
begin: Offset.zero,
end: difference,
).animate(animationController);
animationController.forward();
Expand All @@ -215,8 +223,8 @@ class CardAnimation {
begin: 1.0,
end: scale,
).animate(animationController);
_differenceAnimation = Tween<double>(
begin: 0,
_differenceAnimation = Tween<Offset>(
begin: Offset.zero,
end: difference,
).animate(animationController);
animationController.forward();
Expand Down
19 changes: 15 additions & 4 deletions lib/src/card_swiper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ class CardSwiper extends StatefulWidget {
/// on top of the stack. If the function returns `true`, the undo action is performed as expected.
final CardSwiperOnUndo? onUndo;

/// The offset of the back card from the front card.
///
/// Must be a positive value. Defaults to Offset(0, 40).
final Offset backCardOffset;

const CardSwiper({
Key? key,
required this.cardBuilder,
Expand All @@ -127,6 +132,7 @@ class CardSwiper extends StatefulWidget {
this.isLoop = true,
this.numberOfCardsDisplayed = 2,
this.onUndo,
this.backCardOffset = const Offset(0, 40),
}) : assert(
maxAngle >= 0 && maxAngle <= 360,
'maxAngle must be between 0 and 360',
Expand All @@ -151,6 +157,10 @@ class CardSwiper extends StatefulWidget {
initialIndex >= 0 && initialIndex < cardsCount,
'initialIndex must be between 0 and [cardsCount]',
),
assert(
backCardOffset >= Offset.zero,
'backCardOffset must be a positive value',
),
super(key: key);

@override
Expand Down Expand Up @@ -194,6 +204,7 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper>
initialScale: widget.scale,
isVerticalSwipingEnabled: widget.isVerticalSwipingEnabled,
isHorizontalSwipingEnabled: widget.isHorizontalSwipingEnabled,
initialOffset: widget.backCardOffset,
);
}

Expand Down Expand Up @@ -280,8 +291,8 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper>

Widget _secondItem(BoxConstraints constraints) {
return Positioned(
top: _cardAnimation.difference,
left: 0,
top: _cardAnimation.difference.dy,
left: _cardAnimation.difference.dx,
child: Transform.scale(
scale: _cardAnimation.scale,
child: ConstrainedBox(
Expand All @@ -294,8 +305,8 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper>

Widget _backItem(BoxConstraints constraints, int offset) {
return Positioned(
top: 40,
left: 0,
top: widget.backCardOffset.dy,
left: widget.backCardOffset.dx,
child: Transform.scale(
scale: widget.scale,
child: ConstrainedBox(
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: flutter_card_swiper
description: This is a Tinder-like card swiper package. It allows you to swipe left, right, up, and down and define your own business logic for each direction.
homepage: https://github.com/ricardodalarme/flutter_card_swiper
issue_tracker: https://github.com/ricardodalarme/flutter_card_swiper/issues
version: 4.1.3
version: 5.0.0

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down

0 comments on commit b2dd0e0

Please sign in to comment.