From c912e00c071970aaf9b532abf6f23053f180fe87 Mon Sep 17 00:00:00 2001 From: Ricardo Dalarme Date: Fri, 27 Jan 2023 13:25:37 -0300 Subject: [PATCH] change(perfomance): remove unnecessary setStates --- lib/src/card_swiper.dart | 155 +++++++++++++++++++-------------------- 1 file changed, 76 insertions(+), 79 deletions(-) diff --git a/lib/src/card_swiper.dart b/lib/src/card_swiper.dart index d15525a..faa1f91 100644 --- a/lib/src/card_swiper.dart +++ b/lib/src/card_swiper.dart @@ -295,41 +295,6 @@ class _CardSwiperState extends State } } - //moves the card away to the left or right - void _swipeHorizontal(BuildContext context) { - setState(() { - _swipeType = SwipeType.swipe; - _leftAnimation = Tween( - begin: _left, - end: (_left == 0) - ? (widget.direction == CardSwiperDirection.right) - ? MediaQuery.of(context).size.width - : -MediaQuery.of(context).size.width - : (_left > widget.threshold) - ? MediaQuery.of(context).size.width - : -MediaQuery.of(context).size.width, - ).animate(_animationController); - _topAnimation = Tween( - begin: _top, - end: _top + _top, - ).animate(_animationController); - _scaleAnimation = Tween( - begin: _scale, - end: 1.0, - ).animate(_animationController); - _differenceAnimation = Tween( - begin: _difference, - end: 0, - ).animate(_animationController); - }); - if (_left > widget.threshold || - _left == 0 && widget.direction == CardSwiperDirection.right) { - detectedDirection = CardSwiperDirection.right; - } else { - detectedDirection = CardSwiperDirection.left; - } - } - void _swipe(BuildContext context, CardSwiperDirection direction) { if (widget.cards.isEmpty) return; @@ -356,33 +321,66 @@ class _CardSwiperState extends State _animationController.forward(); } + //moves the card away to the left or right + void _swipeHorizontal(BuildContext context) { + _leftAnimation = Tween( + begin: _left, + end: (_left == 0) + ? (widget.direction == CardSwiperDirection.right) + ? MediaQuery.of(context).size.width + : -MediaQuery.of(context).size.width + : (_left > widget.threshold) + ? MediaQuery.of(context).size.width + : -MediaQuery.of(context).size.width, + ).animate(_animationController); + _topAnimation = Tween( + begin: _top, + end: _top + _top, + ).animate(_animationController); + _scaleAnimation = Tween( + begin: _scale, + end: 1.0, + ).animate(_animationController); + _differenceAnimation = Tween( + begin: _difference, + end: 0, + ).animate(_animationController); + + _swipeType = SwipeType.swipe; + if (_left > widget.threshold || + _left == 0 && widget.direction == CardSwiperDirection.right) { + detectedDirection = CardSwiperDirection.right; + } else { + detectedDirection = CardSwiperDirection.left; + } + } + //moves the card away to the top or bottom void _swipeVertical(BuildContext context) { - setState(() { - _swipeType = SwipeType.swipe; - _leftAnimation = Tween( - begin: _left, - end: _left + _left, - ).animate(_animationController); - _topAnimation = Tween( - begin: _top, - end: (_top == 0) - ? (widget.direction == CardSwiperDirection.bottom) - ? MediaQuery.of(context).size.height - : -MediaQuery.of(context).size.height - : (_top > widget.threshold) - ? MediaQuery.of(context).size.height - : -MediaQuery.of(context).size.height, - ).animate(_animationController); - _scaleAnimation = Tween( - begin: _scale, - end: 1.0, - ).animate(_animationController); - _differenceAnimation = Tween( - begin: _difference, - end: 0, - ).animate(_animationController); - }); + _leftAnimation = Tween( + begin: _left, + end: _left + _left, + ).animate(_animationController); + _topAnimation = Tween( + begin: _top, + end: (_top == 0) + ? (widget.direction == CardSwiperDirection.bottom) + ? MediaQuery.of(context).size.height + : -MediaQuery.of(context).size.height + : (_top > widget.threshold) + ? MediaQuery.of(context).size.height + : -MediaQuery.of(context).size.height, + ).animate(_animationController); + _scaleAnimation = Tween( + begin: _scale, + end: 1.0, + ).animate(_animationController); + _differenceAnimation = Tween( + begin: _difference, + end: 0, + ).animate(_animationController); + + _swipeType = SwipeType.swipe; if (_top > widget.threshold || _top == 0 && widget.direction == CardSwiperDirection.bottom) { detectedDirection = CardSwiperDirection.bottom; @@ -393,24 +391,23 @@ class _CardSwiperState extends State //moves the card back to starting position void _goBack(BuildContext context) { - setState(() { - _swipeType = SwipeType.back; - _leftAnimation = Tween( - begin: _left, - end: 0, - ).animate(_animationController); - _topAnimation = Tween( - begin: _top, - end: 0, - ).animate(_animationController); - _scaleAnimation = Tween( - begin: _scale, - end: 0.9, - ).animate(_animationController); - _differenceAnimation = Tween( - begin: _difference, - end: 40, - ).animate(_animationController); - }); + _leftAnimation = Tween( + begin: _left, + end: 0, + ).animate(_animationController); + _topAnimation = Tween( + begin: _top, + end: 0, + ).animate(_animationController); + _scaleAnimation = Tween( + begin: _scale, + end: 0.9, + ).animate(_animationController); + _differenceAnimation = Tween( + begin: _difference, + end: 40, + ).animate(_animationController); + + _swipeType = SwipeType.back; } }