From 3b868d1875d30b7f30ea3bfffe827202c833eafa Mon Sep 17 00:00:00 2001 From: ricardodalarme Date: Mon, 20 Mar 2023 10:07:04 -0300 Subject: [PATCH] feat: add option to set the initial index --- CHANGELOG.md | 8 ++++++-- README.md | 1 + lib/src/card_swiper.dart | 12 +++++++++++- pubspec.yaml | 2 +- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 29b9d9d..9930ee5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,12 @@ +## [3.1.0] + +- Adds option to set the initial index of the swiper. + ## [3.0.0] - **BREAKING CHANGES**: - - Add currentIndex and previousIndex to the onSwipe callback - - Render the items through a builder function + - Adds currentIndex and previousIndex to the onSwipe callback. + - Renders the items through a builder function. ## [2.1.0] diff --git a/README.md b/README.md index 7164c22..83a32cb 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,7 @@ class Example extends StatelessWidget { | ------------- |:-------------|:-----|:-----:| | cardBuilder | - | Widget builder for rendering cards | true | cardsCount | - | Cards count | true +| initialIndex | 0 | index of the first card when the swiper is initialized | false | controller | - | Trigger swipe | false | padding | EdgeInsets.symmetric(horizontal: 20, vertical: 25) | Control swiper padding | false | duration | 200 milliseconds | The duration that every animation should last | false diff --git a/lib/src/card_swiper.dart b/lib/src/card_swiper.dart index 9ff05d7..23d3ccc 100644 --- a/lib/src/card_swiper.dart +++ b/lib/src/card_swiper.dart @@ -13,6 +13,9 @@ class CardSwiper extends StatefulWidget { /// cards count final int cardsCount; + /// index of the first card when the swiper is initialized + final int initialIndex; + /// controller to trigger actions final CardSwiperController? controller; @@ -63,6 +66,7 @@ class CardSwiper extends StatefulWidget { required this.cardBuilder, required this.cardsCount, this.controller, + this.initialIndex = 0, this.padding = const EdgeInsets.symmetric(horizontal: 20, vertical: 25), this.duration = const Duration(milliseconds: 200), this.maxAngle = 30, @@ -97,6 +101,10 @@ class CardSwiper extends StatefulWidget { numberOfCardsDisplayed >= 1 && numberOfCardsDisplayed <= cardsCount, 'you must display at least one card, and no more than the length of cards parameter', ), + assert( + initialIndex >= 0 && initialIndex < cardsCount, + 'initialIndex must be between 0 and cardsCount', + ), super(key: key); @override @@ -125,7 +133,7 @@ class _CardSwiperState extends State> double get _maxAngle => widget.maxAngle * (pi / 180); - int? _currentIndex = 0; + int? _currentIndex; int? get _nextIndex => getValidIndexOffset(1); bool get _canSwipe => _currentIndex != null && !widget.isDisabled; @@ -133,6 +141,8 @@ class _CardSwiperState extends State> void initState() { super.initState(); + _currentIndex = widget.initialIndex; + widget.controller?.addListener(_controllerListener); _animationController = AnimationController( diff --git a/pubspec.yaml b/pubspec.yaml index 9aa1bc6..35da6d3 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,7 +2,7 @@ name: flutter_card_swiper description: This is a Tinder-like card swiper package. It allows you to swipe left, right, up, and down and define your own business logic for each direction. homepage: https://github.com/ricardodalarme/flutter_card_swiper issue_tracker: https://github.com/ricardodalarme/flutter_card_swiper/issues -version: 3.0.0 +version: 3.1.0 environment: sdk: ">=2.12.0 <3.0.0"