From 0c556f393e587fca31a511838bd934161fde4475 Mon Sep 17 00:00:00 2001 From: Ricardo Dalarme Date: Fri, 13 Jan 2023 21:58:56 -0300 Subject: [PATCH] refactor(code): move the CardsAnimation to a specific file --- lib/src/cards_animation.dart | 81 ++++++++++++++++++++++++++++++ lib/src/swipeable_cards_stack.dart | 79 +---------------------------- 2 files changed, 82 insertions(+), 78 deletions(-) create mode 100644 lib/src/cards_animation.dart diff --git a/lib/src/cards_animation.dart b/lib/src/cards_animation.dart new file mode 100644 index 0000000..2e34e94 --- /dev/null +++ b/lib/src/cards_animation.dart @@ -0,0 +1,81 @@ +import 'package:flutter/widgets.dart'; + +import 'swipeable_cards_stack.dart'; + +class CardsAnimation { + static Animation backCardAlignmentAnim( + AnimationController parent, + ) { + return AlignmentTween(begin: cardsAlign[0], end: cardsAlign[1]).animate( + CurvedAnimation( + parent: parent, + curve: const Interval(0.4, 0.7, curve: Curves.easeIn), + ), + ); + } + + static Animation backCardSizeAnim(AnimationController parent) { + return SizeTween(begin: cardsSize[2], end: cardsSize[1]).animate( + CurvedAnimation( + parent: parent, + curve: const Interval(0.4, 0.7, curve: Curves.easeIn), + ), + ); + } + + static Animation middleCardAlignmentAnim( + AnimationController parent, + ) { + return AlignmentTween(begin: cardsAlign[1], end: cardsAlign[2]).animate( + CurvedAnimation( + parent: parent, + curve: const Interval(0.2, 0.5, curve: Curves.easeIn), + ), + ); + } + + static Animation middleCardSizeAnim(AnimationController parent) { + return SizeTween(begin: cardsSize[1], end: cardsSize[0]).animate( + CurvedAnimation( + parent: parent, + curve: const Interval(0.2, 0.5, curve: Curves.easeIn), + ), + ); + } + + static Animation frontCardDisappearAlignmentAnim( + AnimationController parent, + Alignment beginAlign, + ) { + if (beginAlign.x == -0.001 || + beginAlign.x == 0.001 || + beginAlign.x > 3.0 || + beginAlign.x < -3.0) { + return AlignmentTween( + begin: beginAlign, + end: Alignment( + beginAlign.x > 0 ? beginAlign.x + 30.0 : beginAlign.x - 30.0, + 0.0, + ), // Has swiped to the left or right? + ).animate( + CurvedAnimation( + parent: parent, + curve: const Interval(0.0, 0.5, curve: Curves.easeIn), + ), + ); + } else { + return AlignmentTween( + begin: beginAlign, + end: Alignment( + 0.0, + beginAlign.y > 0 ? beginAlign.y + 30.0 : beginAlign.y - 30.0, + ), // Has swiped to the top or bottom? + ).animate( + CurvedAnimation( + parent: parent, + curve: const Interval(0.0, 0.5, curve: Curves.easeIn), + ), + ); + } + } +} diff --git a/lib/src/swipeable_cards_stack.dart b/lib/src/swipeable_cards_stack.dart index 0d487ac..07a91dc 100644 --- a/lib/src/swipeable_cards_stack.dart +++ b/lib/src/swipeable_cards_stack.dart @@ -3,6 +3,7 @@ library swipeable_cards_stack; import 'dart:math'; import 'package:flutter/material.dart'; +import 'package:swipeable_cards_stack/src/cards_animation.dart'; import 'package:swipeable_cards_stack/src/swipeable_cards_stack_controller.dart'; const List cardsAlign = [ @@ -290,81 +291,3 @@ class _SwipeableCardsStackState extends State ..forward(); } } - -class CardsAnimation { - static Animation backCardAlignmentAnim( - AnimationController parent, - ) { - return AlignmentTween(begin: cardsAlign[0], end: cardsAlign[1]).animate( - CurvedAnimation( - parent: parent, - curve: const Interval(0.4, 0.7, curve: Curves.easeIn), - ), - ); - } - - static Animation backCardSizeAnim(AnimationController parent) { - return SizeTween(begin: cardsSize[2], end: cardsSize[1]).animate( - CurvedAnimation( - parent: parent, - curve: const Interval(0.4, 0.7, curve: Curves.easeIn), - ), - ); - } - - static Animation middleCardAlignmentAnim( - AnimationController parent, - ) { - return AlignmentTween(begin: cardsAlign[1], end: cardsAlign[2]).animate( - CurvedAnimation( - parent: parent, - curve: const Interval(0.2, 0.5, curve: Curves.easeIn), - ), - ); - } - - static Animation middleCardSizeAnim(AnimationController parent) { - return SizeTween(begin: cardsSize[1], end: cardsSize[0]).animate( - CurvedAnimation( - parent: parent, - curve: const Interval(0.2, 0.5, curve: Curves.easeIn), - ), - ); - } - - static Animation frontCardDisappearAlignmentAnim( - AnimationController parent, - Alignment beginAlign, - ) { - if (beginAlign.x == -0.001 || - beginAlign.x == 0.001 || - beginAlign.x > 3.0 || - beginAlign.x < -3.0) { - return AlignmentTween( - begin: beginAlign, - end: Alignment( - beginAlign.x > 0 ? beginAlign.x + 30.0 : beginAlign.x - 30.0, - 0.0, - ), // Has swiped to the left or right? - ).animate( - CurvedAnimation( - parent: parent, - curve: const Interval(0.0, 0.5, curve: Curves.easeIn), - ), - ); - } else { - return AlignmentTween( - begin: beginAlign, - end: Alignment( - 0.0, - beginAlign.y > 0 ? beginAlign.y + 30.0 : beginAlign.y - 30.0, - ), // Has swiped to the top or bottom? - ).animate( - CurvedAnimation( - parent: parent, - curve: const Interval(0.0, 0.5, curve: Curves.easeIn), - ), - ); - } - } -}