feat: allow customize the backcard offset (#12)
This commit is contained in:
parent
852480fb57
commit
b2dd0e001f
|
|
@ -1,5 +1,6 @@
|
||||||
## NEXT
|
## [5.0.0]
|
||||||
|
|
||||||
|
- Adds option to customize the back card offset.
|
||||||
- **BREAKING CHANGE**:
|
- **BREAKING CHANGE**:
|
||||||
- `CardSwiperController` is no longer disposed by `CardSwiper`, but who created it must dispose it.
|
- `CardSwiperController` is no longer disposed by `CardSwiper`, but who created it must dispose it.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -42,9 +42,10 @@ class _ExamplePageState extends State<Example> {
|
||||||
child: CardSwiper(
|
child: CardSwiper(
|
||||||
controller: controller,
|
controller: controller,
|
||||||
cardsCount: cards.length,
|
cardsCount: cards.length,
|
||||||
numberOfCardsDisplayed: 3,
|
|
||||||
onSwipe: _onSwipe,
|
onSwipe: _onSwipe,
|
||||||
onUndo: _onUndo,
|
onUndo: _onUndo,
|
||||||
|
numberOfCardsDisplayed: 3,
|
||||||
|
backCardOffset: const Offset(40, 40),
|
||||||
padding: const EdgeInsets.all(24.0),
|
padding: const EdgeInsets.all(24.0),
|
||||||
cardBuilder: (context, index) => cards[index],
|
cardBuilder: (context, index) => cards[index],
|
||||||
),
|
),
|
||||||
|
|
|
||||||
|
|
@ -9,12 +9,15 @@ class CardAnimation {
|
||||||
required this.animationController,
|
required this.animationController,
|
||||||
required this.maxAngle,
|
required this.maxAngle,
|
||||||
required this.initialScale,
|
required this.initialScale,
|
||||||
|
required this.initialOffset,
|
||||||
this.isHorizontalSwipingEnabled = true,
|
this.isHorizontalSwipingEnabled = true,
|
||||||
this.isVerticalSwipingEnabled = true,
|
this.isVerticalSwipingEnabled = true,
|
||||||
}) : scale = initialScale;
|
}) : scale = initialScale,
|
||||||
|
difference = initialOffset;
|
||||||
|
|
||||||
final double maxAngle;
|
final double maxAngle;
|
||||||
final double initialScale;
|
final double initialScale;
|
||||||
|
final Offset initialOffset;
|
||||||
final AnimationController animationController;
|
final AnimationController animationController;
|
||||||
final bool isHorizontalSwipingEnabled;
|
final bool isHorizontalSwipingEnabled;
|
||||||
final bool isVerticalSwipingEnabled;
|
final bool isVerticalSwipingEnabled;
|
||||||
|
|
@ -24,12 +27,12 @@ class CardAnimation {
|
||||||
double total = 0;
|
double total = 0;
|
||||||
double angle = 0;
|
double angle = 0;
|
||||||
double scale;
|
double scale;
|
||||||
double difference = 40;
|
Offset difference;
|
||||||
|
|
||||||
late Animation<double> _leftAnimation;
|
late Animation<double> _leftAnimation;
|
||||||
late Animation<double> _topAnimation;
|
late Animation<double> _topAnimation;
|
||||||
late Animation<double> _scaleAnimation;
|
late Animation<double> _scaleAnimation;
|
||||||
late Animation<double> _differenceAnimation;
|
late Animation<Offset> _differenceAnimation;
|
||||||
|
|
||||||
double get _maxAngleInRadian => maxAngle * (pi / 180);
|
double get _maxAngleInRadian => maxAngle * (pi / 180);
|
||||||
|
|
||||||
|
|
@ -47,7 +50,7 @@ class CardAnimation {
|
||||||
total = 0;
|
total = 0;
|
||||||
angle = 0;
|
angle = 0;
|
||||||
scale = initialScale;
|
scale = initialScale;
|
||||||
difference = 40;
|
difference = initialOffset;
|
||||||
}
|
}
|
||||||
|
|
||||||
void update(double dx, double dy, bool inverseAngle) {
|
void update(double dx, double dy, bool inverseAngle) {
|
||||||
|
|
@ -79,8 +82,13 @@ class CardAnimation {
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateDifference() {
|
void updateDifference() {
|
||||||
if (difference.isBetween(0, difference)) {
|
final discrepancy = (total > 0) ? total / 10 : -(total / 10);
|
||||||
difference = (total > 0) ? 40 - (total / 10) : 40 + (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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -114,9 +122,9 @@ class CardAnimation {
|
||||||
begin: scale,
|
begin: scale,
|
||||||
end: 1.0,
|
end: 1.0,
|
||||||
).animate(animationController);
|
).animate(animationController);
|
||||||
_differenceAnimation = Tween<double>(
|
_differenceAnimation = Tween<Offset>(
|
||||||
begin: difference,
|
begin: difference,
|
||||||
end: 0,
|
end: Offset.zero,
|
||||||
).animate(animationController);
|
).animate(animationController);
|
||||||
animationController.forward();
|
animationController.forward();
|
||||||
}
|
}
|
||||||
|
|
@ -136,9 +144,9 @@ class CardAnimation {
|
||||||
begin: scale,
|
begin: scale,
|
||||||
end: 1.0,
|
end: 1.0,
|
||||||
).animate(animationController);
|
).animate(animationController);
|
||||||
_differenceAnimation = Tween<double>(
|
_differenceAnimation = Tween<Offset>(
|
||||||
begin: difference,
|
begin: difference,
|
||||||
end: 0,
|
end: Offset.zero,
|
||||||
).animate(animationController);
|
).animate(animationController);
|
||||||
animationController.forward();
|
animationController.forward();
|
||||||
}
|
}
|
||||||
|
|
@ -156,9 +164,9 @@ class CardAnimation {
|
||||||
begin: scale,
|
begin: scale,
|
||||||
end: initialScale,
|
end: initialScale,
|
||||||
).animate(animationController);
|
).animate(animationController);
|
||||||
_differenceAnimation = Tween<double>(
|
_differenceAnimation = Tween<Offset>(
|
||||||
begin: difference,
|
begin: difference,
|
||||||
end: 40,
|
end: initialOffset,
|
||||||
).animate(animationController);
|
).animate(animationController);
|
||||||
animationController.forward();
|
animationController.forward();
|
||||||
}
|
}
|
||||||
|
|
@ -193,8 +201,8 @@ class CardAnimation {
|
||||||
begin: 1.0,
|
begin: 1.0,
|
||||||
end: scale,
|
end: scale,
|
||||||
).animate(animationController);
|
).animate(animationController);
|
||||||
_differenceAnimation = Tween<double>(
|
_differenceAnimation = Tween<Offset>(
|
||||||
begin: 0,
|
begin: Offset.zero,
|
||||||
end: difference,
|
end: difference,
|
||||||
).animate(animationController);
|
).animate(animationController);
|
||||||
animationController.forward();
|
animationController.forward();
|
||||||
|
|
@ -215,8 +223,8 @@ class CardAnimation {
|
||||||
begin: 1.0,
|
begin: 1.0,
|
||||||
end: scale,
|
end: scale,
|
||||||
).animate(animationController);
|
).animate(animationController);
|
||||||
_differenceAnimation = Tween<double>(
|
_differenceAnimation = Tween<Offset>(
|
||||||
begin: 0,
|
begin: Offset.zero,
|
||||||
end: difference,
|
end: difference,
|
||||||
).animate(animationController);
|
).animate(animationController);
|
||||||
animationController.forward();
|
animationController.forward();
|
||||||
|
|
|
||||||
|
|
@ -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.
|
/// on top of the stack. If the function returns `true`, the undo action is performed as expected.
|
||||||
final CardSwiperOnUndo? onUndo;
|
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({
|
const CardSwiper({
|
||||||
Key? key,
|
Key? key,
|
||||||
required this.cardBuilder,
|
required this.cardBuilder,
|
||||||
|
|
@ -127,6 +132,7 @@ class CardSwiper extends StatefulWidget {
|
||||||
this.isLoop = true,
|
this.isLoop = true,
|
||||||
this.numberOfCardsDisplayed = 2,
|
this.numberOfCardsDisplayed = 2,
|
||||||
this.onUndo,
|
this.onUndo,
|
||||||
|
this.backCardOffset = const Offset(0, 40),
|
||||||
}) : assert(
|
}) : assert(
|
||||||
maxAngle >= 0 && maxAngle <= 360,
|
maxAngle >= 0 && maxAngle <= 360,
|
||||||
'maxAngle must be between 0 and 360',
|
'maxAngle must be between 0 and 360',
|
||||||
|
|
@ -151,6 +157,10 @@ class CardSwiper extends StatefulWidget {
|
||||||
initialIndex >= 0 && initialIndex < cardsCount,
|
initialIndex >= 0 && initialIndex < cardsCount,
|
||||||
'initialIndex must be between 0 and [cardsCount]',
|
'initialIndex must be between 0 and [cardsCount]',
|
||||||
),
|
),
|
||||||
|
assert(
|
||||||
|
backCardOffset >= Offset.zero,
|
||||||
|
'backCardOffset must be a positive value',
|
||||||
|
),
|
||||||
super(key: key);
|
super(key: key);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|
@ -194,6 +204,7 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper>
|
||||||
initialScale: widget.scale,
|
initialScale: widget.scale,
|
||||||
isVerticalSwipingEnabled: widget.isVerticalSwipingEnabled,
|
isVerticalSwipingEnabled: widget.isVerticalSwipingEnabled,
|
||||||
isHorizontalSwipingEnabled: widget.isHorizontalSwipingEnabled,
|
isHorizontalSwipingEnabled: widget.isHorizontalSwipingEnabled,
|
||||||
|
initialOffset: widget.backCardOffset,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -280,8 +291,8 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper>
|
||||||
|
|
||||||
Widget _secondItem(BoxConstraints constraints) {
|
Widget _secondItem(BoxConstraints constraints) {
|
||||||
return Positioned(
|
return Positioned(
|
||||||
top: _cardAnimation.difference,
|
top: _cardAnimation.difference.dy,
|
||||||
left: 0,
|
left: _cardAnimation.difference.dx,
|
||||||
child: Transform.scale(
|
child: Transform.scale(
|
||||||
scale: _cardAnimation.scale,
|
scale: _cardAnimation.scale,
|
||||||
child: ConstrainedBox(
|
child: ConstrainedBox(
|
||||||
|
|
@ -294,8 +305,8 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper>
|
||||||
|
|
||||||
Widget _backItem(BoxConstraints constraints, int offset) {
|
Widget _backItem(BoxConstraints constraints, int offset) {
|
||||||
return Positioned(
|
return Positioned(
|
||||||
top: 40,
|
top: widget.backCardOffset.dy,
|
||||||
left: 0,
|
left: widget.backCardOffset.dx,
|
||||||
child: Transform.scale(
|
child: Transform.scale(
|
||||||
scale: widget.scale,
|
scale: widget.scale,
|
||||||
child: ConstrainedBox(
|
child: ConstrainedBox(
|
||||||
|
|
|
||||||
|
|
@ -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.
|
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
|
homepage: https://github.com/ricardodalarme/flutter_card_swiper
|
||||||
issue_tracker: https://github.com/ricardodalarme/flutter_card_swiper/issues
|
issue_tracker: https://github.com/ricardodalarme/flutter_card_swiper/issues
|
||||||
version: 4.1.3
|
version: 5.0.0
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
sdk: ">=2.12.0 <3.0.0"
|
sdk: ">=2.12.0 <3.0.0"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue