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,33 +321,66 @@ 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(() { _leftAnimation = Tween<double>(
_swipeType = SwipeType.swipe; begin: _left,
_leftAnimation = Tween<double>( end: _left + _left,
begin: _left, ).animate(_animationController);
end: _left + _left, _topAnimation = Tween<double>(
).animate(_animationController); begin: _top,
_topAnimation = Tween<double>( end: (_top == 0)
begin: _top, ? (widget.direction == CardSwiperDirection.bottom)
end: (_top == 0) ? MediaQuery.of(context).size.height
? (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
: (_top > widget.threshold) : -MediaQuery.of(context).size.height,
? MediaQuery.of(context).size.height ).animate(_animationController);
: -MediaQuery.of(context).size.height, _scaleAnimation = Tween<double>(
).animate(_animationController); begin: _scale,
_scaleAnimation = Tween<double>( end: 1.0,
begin: _scale, ).animate(_animationController);
end: 1.0, _differenceAnimation = Tween<double>(
).animate(_animationController); begin: _difference,
_differenceAnimation = Tween<double>( end: 0,
begin: _difference, ).animate(_animationController);
end: 0,
).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,24 +391,23 @@ 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(() { _leftAnimation = Tween<double>(
_swipeType = SwipeType.back; begin: _left,
_leftAnimation = Tween<double>( end: 0,
begin: _left, ).animate(_animationController);
end: 0, _topAnimation = Tween<double>(
).animate(_animationController); begin: _top,
_topAnimation = Tween<double>( end: 0,
begin: _top, ).animate(_animationController);
end: 0, _scaleAnimation = Tween<double>(
).animate(_animationController); begin: _scale,
_scaleAnimation = Tween<double>( end: 0.9,
begin: _scale, ).animate(_animationController);
end: 0.9, _differenceAnimation = Tween<double>(
).animate(_animationController); begin: _difference,
_differenceAnimation = Tween<double>( end: 40,
begin: _difference, ).animate(_animationController);
end: 40,
).animate(_animationController); _swipeType = SwipeType.back;
});
} }
} }