change(perfomance): remove unnecessary setStates

This commit is contained in:
Ricardo Dalarme 2023-01-27 13:25:37 -03:00
parent ee6f662dfd
commit c912e00c07
1 changed files with 76 additions and 79 deletions

View File

@ -295,41 +295,6 @@ class _CardSwiperState extends State<CardSwiper>
} }
} }
//moves the card away to the left or right
void _swipeHorizontal(BuildContext context) {
setState(() {
_swipeType = SwipeType.swipe;
_leftAnimation = Tween<double>(
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<double>(
begin: _top,
end: _top + _top,
).animate(_animationController);
_scaleAnimation = Tween<double>(
begin: _scale,
end: 1.0,
).animate(_animationController);
_differenceAnimation = Tween<double>(
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) { void _swipe(BuildContext context, CardSwiperDirection direction) {
if (widget.cards.isEmpty) return; if (widget.cards.isEmpty) return;
@ -356,10 +321,42 @@ class _CardSwiperState extends State<CardSwiper>
_animationController.forward(); _animationController.forward();
} }
//moves the card away to the left or right
void _swipeHorizontal(BuildContext context) {
_leftAnimation = Tween<double>(
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<double>(
begin: _top,
end: _top + _top,
).animate(_animationController);
_scaleAnimation = Tween<double>(
begin: _scale,
end: 1.0,
).animate(_animationController);
_differenceAnimation = Tween<double>(
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 //moves the card away to the top or bottom
void _swipeVertical(BuildContext context) { void _swipeVertical(BuildContext context) {
setState(() {
_swipeType = SwipeType.swipe;
_leftAnimation = Tween<double>( _leftAnimation = Tween<double>(
begin: _left, begin: _left,
end: _left + _left, end: _left + _left,
@ -382,7 +379,8 @@ class _CardSwiperState extends State<CardSwiper>
begin: _difference, begin: _difference,
end: 0, end: 0,
).animate(_animationController); ).animate(_animationController);
});
_swipeType = SwipeType.swipe;
if (_top > widget.threshold || if (_top > widget.threshold ||
_top == 0 && widget.direction == CardSwiperDirection.bottom) { _top == 0 && widget.direction == CardSwiperDirection.bottom) {
detectedDirection = CardSwiperDirection.bottom; detectedDirection = CardSwiperDirection.bottom;
@ -393,8 +391,6 @@ class _CardSwiperState extends State<CardSwiper>
//moves the card back to starting position //moves the card back to starting position
void _goBack(BuildContext context) { void _goBack(BuildContext context) {
setState(() {
_swipeType = SwipeType.back;
_leftAnimation = Tween<double>( _leftAnimation = Tween<double>(
begin: _left, begin: _left,
end: 0, end: 0,
@ -411,6 +407,7 @@ class _CardSwiperState extends State<CardSwiper>
begin: _difference, begin: _difference,
end: 40, end: 40,
).animate(_animationController); ).animate(_animationController);
});
_swipeType = SwipeType.back;
} }
} }