feat: add option to set the initial index

This commit is contained in:
ricardodalarme 2023-03-20 10:07:04 -03:00
parent db500bf458
commit 3b868d1875
4 changed files with 19 additions and 4 deletions

View File

@ -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]

View File

@ -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

View File

@ -13,6 +13,9 @@ class CardSwiper<T extends Widget> 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<T extends Widget> 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<T extends Widget> 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<T extends Widget> extends State<CardSwiper<T>>
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<T extends Widget> extends State<CardSwiper<T>>
void initState() {
super.initState();
_currentIndex = widget.initialIndex;
widget.controller?.addListener(_controllerListener);
_animationController = AnimationController(

View File

@ -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"