feat(stack): make a infinite stack
This commit is contained in:
parent
ead2b3f8b5
commit
fb292ee1ec
|
|
@ -80,6 +80,8 @@ class _CardSwiperState extends State<CardSwiper>
|
||||||
double _scale = 0.9;
|
double _scale = 0.9;
|
||||||
double _difference = 40;
|
double _difference = 40;
|
||||||
|
|
||||||
|
int _currentIndex = 0;
|
||||||
|
|
||||||
int _swipeTyp = 0; // 1 = swipe, 2 = unswipe, 3 = goBack
|
int _swipeTyp = 0; // 1 = swipe, 2 = unswipe, 3 = goBack
|
||||||
bool _tapOnTop = false; //position of starting drag point on card
|
bool _tapOnTop = false; //position of starting drag point on card
|
||||||
|
|
||||||
|
|
@ -102,6 +104,9 @@ class _CardSwiperState extends State<CardSwiper>
|
||||||
List<AppinioUnswipeCard?> _lastCards = [];
|
List<AppinioUnswipeCard?> _lastCards = [];
|
||||||
CardSwiperDirection detectedDirection = CardSwiperDirection.none;
|
CardSwiperDirection detectedDirection = CardSwiperDirection.none;
|
||||||
|
|
||||||
|
bool get _isLastCard => _currentIndex == widget.cards!.length - 1;
|
||||||
|
int get _nextCardIndex => _isLastCard ? 0 : _currentIndex + 1;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
|
|
@ -226,10 +231,15 @@ class _CardSwiperState extends State<CardSwiper>
|
||||||
_swipedDirectionVertical = 0;
|
_swipedDirectionVertical = 0;
|
||||||
_vertical = false;
|
_vertical = false;
|
||||||
_horizontal = false;
|
_horizontal = false;
|
||||||
widget.cards!.removeLast();
|
|
||||||
|
|
||||||
widget.onSwipe(widget.cards!.length, detectedDirection);
|
widget.onSwipe(_currentIndex, detectedDirection);
|
||||||
if (widget.cards!.isEmpty) widget.onEnd();
|
|
||||||
|
if (_isLastCard) {
|
||||||
|
widget.onEnd();
|
||||||
|
_currentIndex = 0;
|
||||||
|
} else {
|
||||||
|
_currentIndex++;
|
||||||
|
}
|
||||||
} else if (_swipeTyp == 2) {
|
} else if (_swipeTyp == 2) {
|
||||||
if (widget.unlimitedUnswipe) {
|
if (widget.unlimitedUnswipe) {
|
||||||
_lastCards.removeLast();
|
_lastCards.removeLast();
|
||||||
|
|
@ -269,16 +279,8 @@ class _CardSwiperState extends State<CardSwiper>
|
||||||
clipBehavior: Clip.none,
|
clipBehavior: Clip.none,
|
||||||
fit: StackFit.expand,
|
fit: StackFit.expand,
|
||||||
children: [
|
children: [
|
||||||
...widget.cards!
|
_backItem(constraints, _nextCardIndex),
|
||||||
.asMap()
|
_frontItem(constraints, _currentIndex)
|
||||||
.map((index, _) {
|
|
||||||
return MapEntry(
|
|
||||||
index,
|
|
||||||
_item(constraints, index),
|
|
||||||
);
|
|
||||||
})
|
|
||||||
.values
|
|
||||||
.toList(),
|
|
||||||
]);
|
]);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
|
@ -287,27 +289,7 @@ class _CardSwiperState extends State<CardSwiper>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _item(BoxConstraints constraints, int index) {
|
Widget _frontItem(BoxConstraints constraints, int index) {
|
||||||
if (index != widget.cards!.length - 1) {
|
|
||||||
return Visibility(
|
|
||||||
visible: widget.cards!.length - index <= 2,
|
|
||||||
child: Positioned(
|
|
||||||
top: _difference,
|
|
||||||
left: 0,
|
|
||||||
child: Container(
|
|
||||||
color: Colors.transparent,
|
|
||||||
child: Transform.scale(
|
|
||||||
scale: _scale,
|
|
||||||
child: Container(
|
|
||||||
constraints: constraints,
|
|
||||||
child: widget.cards![index],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Positioned(
|
return Positioned(
|
||||||
left: _left,
|
left: _left,
|
||||||
top: _top,
|
top: _top,
|
||||||
|
|
@ -355,6 +337,26 @@ class _CardSwiperState extends State<CardSwiper>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Widget _backItem(BoxConstraints constraints, int index) {
|
||||||
|
return Visibility(
|
||||||
|
visible: widget.cards!.length - index <= 2,
|
||||||
|
child: Positioned(
|
||||||
|
top: _difference,
|
||||||
|
left: 0,
|
||||||
|
child: Container(
|
||||||
|
color: Colors.transparent,
|
||||||
|
child: Transform.scale(
|
||||||
|
scale: _scale,
|
||||||
|
child: Container(
|
||||||
|
constraints: constraints,
|
||||||
|
child: widget.cards![index],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
void _calculateAngle() {
|
void _calculateAngle() {
|
||||||
if (_angle <= _maxAngle && _angle >= -_maxAngle) {
|
if (_angle <= _maxAngle && _angle >= -_maxAngle) {
|
||||||
(_tapOnTop == true)
|
(_tapOnTop == true)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue