From 7b79e303f72ae7e16738234469aa77d9a9ac9ceb Mon Sep 17 00:00:00 2001 From: ricardodalarme Date: Sun, 28 Jan 2024 13:32:18 -0300 Subject: [PATCH] chore: upgrade min sdk version to 3.0.0 --- CHANGELOG.md | 5 +++ example/lib/example_card.dart | 4 +-- example/lib/main.dart | 8 ++--- example/pubspec.yaml | 2 +- lib/src/card_animation.dart | 38 ++++++++------------ lib/src/utils/direction_extension.dart | 19 ++++------ lib/src/widget/card_swiper.dart | 5 ++- lib/src/widget/card_swiper_state.dart | 50 ++++++++++---------------- pubspec.yaml | 2 +- 9 files changed, 53 insertions(+), 80 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 11328b0..48923f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,9 @@ +## [7.0.0] + +- **BREAKING CHANGE**: + - Upgrade min dart sdk to 3.0.0 + ## [6.1.0] - Fixes cannot access context in onSwipe when no cards left then "destroy" the widget tree diff --git a/example/lib/example_card.dart b/example/lib/example_card.dart index edfd6e5..557da08 100644 --- a/example/lib/example_card.dart +++ b/example/lib/example_card.dart @@ -6,8 +6,8 @@ class ExampleCard extends StatelessWidget { const ExampleCard( this.candidate, { - Key? key, - }) : super(key: key); + super.key, + }); @override Widget build(BuildContext context) { diff --git a/example/lib/main.dart b/example/lib/main.dart index 5e22383..c8d8f72 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -14,8 +14,8 @@ void main() { class Example extends StatefulWidget { const Example({ - Key? key, - }) : super(key: key); + super.key, + }); @override State createState() => _ExamplePageState(); @@ -65,10 +65,6 @@ class _ExamplePageState extends State { onPressed: controller.undo, child: const Icon(Icons.rotate_left), ), - FloatingActionButton( - onPressed: controller.swipe, - child: const Icon(Icons.rotate_right), - ), FloatingActionButton( onPressed: controller.swipeLeft, child: const Icon(Icons.keyboard_arrow_left), diff --git a/example/pubspec.yaml b/example/pubspec.yaml index ed72635..83f6daa 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -5,7 +5,7 @@ publish_to: "none" version: 0.0.1 environment: - sdk: ">=2.15.0 <4.0.0" + sdk: ">=3.0.0 <4.0.0" dependencies: flutter: diff --git a/lib/src/card_animation.dart b/lib/src/card_animation.dart index c19c6f5..eba4cf4 100644 --- a/lib/src/card_animation.dart +++ b/lib/src/card_animation.dart @@ -136,18 +136,13 @@ class CardAnimation { } void animate(BuildContext context, CardSwiperDirection direction) { - switch (direction) { - case CardSwiperDirection.left: - return animateHorizontally(context, false); - case CardSwiperDirection.right: - return animateHorizontally(context, true); - case CardSwiperDirection.top: - return animateVertically(context, false); - case CardSwiperDirection.bottom: - return animateVertically(context, true); - default: - return; - } + return switch (direction) { + CardSwiperDirection.left => animateHorizontally(context, false), + CardSwiperDirection.right => animateHorizontally(context, true), + CardSwiperDirection.top => animateVertically(context, false), + CardSwiperDirection.bottom => animateVertically(context, true), + CardSwiperDirection.none => null, + }; } void animateHorizontally(BuildContext context, bool isToRight) { @@ -215,18 +210,13 @@ class CardAnimation { } void animateUndo(BuildContext context, CardSwiperDirection direction) { - switch (direction) { - case CardSwiperDirection.left: - return animateUndoHorizontally(context, false); - case CardSwiperDirection.right: - return animateUndoHorizontally(context, true); - case CardSwiperDirection.top: - return animateUndoVertically(context, false); - case CardSwiperDirection.bottom: - return animateUndoVertically(context, true); - default: - return; - } + return switch (direction) { + CardSwiperDirection.left => animateUndoHorizontally(context, false), + CardSwiperDirection.right => animateUndoHorizontally(context, true), + CardSwiperDirection.top => animateUndoVertically(context, false), + CardSwiperDirection.bottom => animateUndoVertically(context, true), + _ => null + }; } void animateUndoHorizontally(BuildContext context, bool isToRight) { diff --git a/lib/src/utils/direction_extension.dart b/lib/src/utils/direction_extension.dart index 6ffad3b..daa61e0 100644 --- a/lib/src/utils/direction_extension.dart +++ b/lib/src/utils/direction_extension.dart @@ -2,16 +2,11 @@ import 'package:flutter/widgets.dart'; import 'package:flutter_card_swiper/src/enums.dart'; extension DirectionExtension on CardSwiperDirection { - Axis get axis { - switch (this) { - case CardSwiperDirection.left: - case CardSwiperDirection.right: - return Axis.horizontal; - case CardSwiperDirection.top: - case CardSwiperDirection.bottom: - return Axis.vertical; - case CardSwiperDirection.none: - throw Exception('Direction is none'); - } - } + Axis get axis => switch (this) { + CardSwiperDirection.left || + CardSwiperDirection.right => + Axis.horizontal, + CardSwiperDirection.top || CardSwiperDirection.bottom => Axis.vertical, + CardSwiperDirection.none => throw Exception('Direction is none'), + }; } diff --git a/lib/src/widget/card_swiper.dart b/lib/src/widget/card_swiper.dart index 7fa7317..dac2ab6 100644 --- a/lib/src/widget/card_swiper.dart +++ b/lib/src/widget/card_swiper.dart @@ -131,7 +131,6 @@ class CardSwiper extends StatefulWidget { const CardSwiper({ required this.cardBuilder, required this.cardsCount, - Key? key, this.controller, this.initialIndex = 0, this.padding = const EdgeInsets.symmetric(horizontal: 20, vertical: 25), @@ -150,6 +149,7 @@ class CardSwiper extends StatefulWidget { this.numberOfCardsDisplayed = 2, this.onUndo, this.backCardOffset = const Offset(0, 40), + super.key, }) : assert( maxAngle >= 0 && maxAngle <= 360, 'maxAngle must be between 0 and 360', @@ -173,8 +173,7 @@ class CardSwiper extends StatefulWidget { assert( initialIndex >= 0 && initialIndex < cardsCount, 'initialIndex must be between 0 and [cardsCount]', - ), - super(key: key); + ); @override State createState() => _CardSwiperState(); diff --git a/lib/src/widget/card_swiper_state.dart b/lib/src/widget/card_swiper_state.dart index b80b08c..64befea 100644 --- a/lib/src/widget/card_swiper_state.dart +++ b/lib/src/widget/card_swiper_state.dart @@ -46,26 +46,20 @@ class _CardSwiperState extends State } void onSwipeDirectionChanged(CardSwiperDirection direction) { - if (direction == CardSwiperDirection.none) { - _detectedVerticalDirection = direction; - _detectedHorizontalDirection = direction; - widget.onSwipeDirectionChange - ?.call(_detectedHorizontalDirection, _detectedVerticalDirection); - } else if (direction == CardSwiperDirection.right || - direction == CardSwiperDirection.left) { - if (_detectedHorizontalDirection != direction) { - _detectedHorizontalDirection = direction; - widget.onSwipeDirectionChange - ?.call(_detectedHorizontalDirection, _detectedVerticalDirection); - } - } else if (direction == CardSwiperDirection.top || - direction == CardSwiperDirection.bottom) { - if (_detectedVerticalDirection != direction) { + switch (direction) { + case CardSwiperDirection.none: + _detectedVerticalDirection = direction; + _detectedHorizontalDirection = direction; + case CardSwiperDirection.right: + case CardSwiperDirection.left: + _detectedHorizontalDirection = direction; + case CardSwiperDirection.top: + case CardSwiperDirection.bottom: _detectedVerticalDirection = direction; - widget.onSwipeDirectionChange - ?.call(_detectedHorizontalDirection, _detectedVerticalDirection); - } } + + widget.onSwipeDirectionChange + ?.call(_detectedHorizontalDirection, _detectedVerticalDirection); } @override @@ -193,7 +187,6 @@ class _CardSwiperState extends State switch (_swipeType) { case SwipeType.swipe: await _handleCompleteSwipe(); - break; default: break; } @@ -256,18 +249,13 @@ class _CardSwiperState extends State } bool _isValidDirection(CardSwiperDirection direction) { - switch (direction) { - case CardSwiperDirection.left: - return widget.allowedSwipeDirection.left; - case CardSwiperDirection.right: - return widget.allowedSwipeDirection.right; - case CardSwiperDirection.top: - return widget.allowedSwipeDirection.up; - case CardSwiperDirection.bottom: - return widget.allowedSwipeDirection.down; - default: - return false; - } + return switch (direction) { + CardSwiperDirection.left => widget.allowedSwipeDirection.left, + CardSwiperDirection.right => widget.allowedSwipeDirection.right, + CardSwiperDirection.top => widget.allowedSwipeDirection.up, + CardSwiperDirection.bottom => widget.allowedSwipeDirection.down, + _ => false + }; } void _swipe(CardSwiperDirection direction) { diff --git a/pubspec.yaml b/pubspec.yaml index 967c868..79b31b1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -5,7 +5,7 @@ issue_tracker: https://github.com/ricardodalarme/flutter_card_swiper/issues version: 6.1.0 environment: - sdk: ">=2.12.0 <4.0.0" + sdk: ">=3.0.0 <4.0.0" flutter: ">=1.17.0" dependencies: