diff --git a/lib/src/card_animation.dart b/lib/src/card_animation.dart index 0c88aea..6ccba0a 100644 --- a/lib/src/card_animation.dart +++ b/lib/src/card_animation.dart @@ -1,9 +1,8 @@ -import 'dart:math'; +import 'dart:math' as math; import 'dart:ui'; import 'package:flutter/widgets.dart'; import 'package:flutter_card_swiper/flutter_card_swiper.dart'; -import 'package:flutter_card_swiper/src/extensions.dart'; class CardAnimation { CardAnimation({ @@ -13,8 +12,7 @@ class CardAnimation { required this.initialOffset, this.isHorizontalSwipingEnabled = true, this.isVerticalSwipingEnabled = true, - }) : scale = initialScale, - difference = initialOffset; + }) : scale = initialScale; final double maxAngle; final double initialScale; @@ -28,14 +26,14 @@ class CardAnimation { double total = 0; double angle = 0; double scale; - Offset difference; + Offset difference = Offset.zero; late Animation _leftAnimation; late Animation _topAnimation; late Animation _scaleAnimation; late Animation _differenceAnimation; - double get _maxAngleInRadian => maxAngle * (pi / 180); + double get _maxAngleInRadian => maxAngle * (math.pi / 180); void sync() { left = _leftAnimation.value; @@ -51,7 +49,7 @@ class CardAnimation { total = 0; angle = 0; scale = initialScale; - difference = initialOffset; + difference = Offset.zero; } void update(double dx, double dy, bool inverseAngle) { @@ -68,27 +66,36 @@ class CardAnimation { } void updateAngle(bool inverse) { - if (angle.isBetween(-_maxAngleInRadian, _maxAngleInRadian)) { - angle = _maxAngleInRadian * left / 1000; - if (inverse) angle *= -1; - } + angle = clampDouble( + _maxAngleInRadian * left / 1000, + -_maxAngleInRadian, + _maxAngleInRadian, + ); + if (inverse) angle *= -1; } void updateScale() { - scale = (total > 0) - ? clampDouble(initialScale + (total / 5000), initialScale, 1.0) - : clampDouble(initialScale - (total / 5000), initialScale, 1.0); + scale = clampDouble(initialScale + (total.abs() / 5000), initialScale, 1.0); } void updateDifference() { - final discrepancy = (total > 0) ? total / 10 : -(total / 10); + final discrepancy = (total / 10).abs(); - if (difference.dx.isBetween(0, initialOffset.dx)) { - difference = Offset(initialOffset.dx + discrepancy, difference.dy); + var diffX = 0.0, diffY = 0.0; + + if (initialOffset.dx > 0) { + diffX = discrepancy; + } else if (initialOffset.dx < 0) { + diffX = -discrepancy; } - if (difference.dy.isBetween(0, initialOffset.dy)) { - difference = Offset(difference.dx, initialOffset.dy + discrepancy); + + if (initialOffset.dy < 0) { + diffY = -discrepancy; + } else if (initialOffset.dy > 0) { + diffY = discrepancy; } + + difference = Offset(diffX, diffY); } void animate(BuildContext context, CardSwiperDirection direction) { @@ -123,7 +130,7 @@ class CardAnimation { ).animate(animationController); _differenceAnimation = Tween( begin: difference, - end: Offset.zero, + end: initialOffset, ).animate(animationController); animationController.forward(); } @@ -145,7 +152,7 @@ class CardAnimation { ).animate(animationController); _differenceAnimation = Tween( begin: difference, - end: Offset.zero, + end: initialOffset, ).animate(animationController); animationController.forward(); } @@ -165,7 +172,7 @@ class CardAnimation { ).animate(animationController); _differenceAnimation = Tween( begin: difference, - end: initialOffset, + end: Offset.zero, ).animate(animationController); animationController.forward(); } @@ -201,7 +208,7 @@ class CardAnimation { end: scale, ).animate(animationController); _differenceAnimation = Tween( - begin: Offset.zero, + begin: initialOffset, end: difference, ).animate(animationController); animationController.forward(); @@ -223,7 +230,7 @@ class CardAnimation { end: scale, ).animate(animationController); _differenceAnimation = Tween( - begin: Offset.zero, + begin: initialOffset, end: difference, ).animate(animationController); animationController.forward(); diff --git a/lib/src/card_swiper.dart b/lib/src/card_swiper.dart index bde5257..6a075de 100644 --- a/lib/src/card_swiper.dart +++ b/lib/src/card_swiper.dart @@ -1,5 +1,5 @@ import 'dart:collection'; -import 'dart:math'; +import 'dart:math' as math; import 'package:flutter/widgets.dart'; import 'package:flutter_card_swiper/src/card_animation.dart'; @@ -157,10 +157,6 @@ 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 @@ -227,9 +223,7 @@ class _CardSwiperState extends State clipBehavior: Clip.none, fit: StackFit.expand, children: List.generate(numberOfCardsOnScreen(), (index) { - if (index == 0) { - return _frontItem(constraints); - } + if (index == 0) return _frontItem(constraints); return _backItem(constraints, index); }).reversed.toList(), @@ -287,15 +281,15 @@ class _CardSwiperState extends State ); } - Widget _backItem(BoxConstraints constraints, int offset) { + Widget _backItem(BoxConstraints constraints, int index) { return Positioned( - top: _cardAnimation.difference.dy * offset, - left: _cardAnimation.difference.dx * offset, + top: (widget.backCardOffset.dy * index) - _cardAnimation.difference.dy, + left: (widget.backCardOffset.dx * index) - _cardAnimation.difference.dx, child: Transform.scale( - scale: _cardAnimation.scale - ((1 - widget.scale) * (offset - 1)), + scale: _cardAnimation.scale - ((1 - widget.scale) * (index - 1)), child: ConstrainedBox( constraints: constraints, - child: widget.cardBuilder(context, getValidIndexOffset(offset)!), + child: widget.cardBuilder(context, getValidIndexOffset(index)!), ), ), ); @@ -426,7 +420,7 @@ class _CardSwiperState extends State return 0; } - return min( + return math.min( widget.numberOfCardsDisplayed, widget.cardsCount - _currentIndex!, );