feat(controller): allow swiping up and down
This commit is contained in:
parent
c8f044cc7d
commit
06ac501f9d
|
|
@ -1,3 +1,7 @@
|
|||
## [1.1.0]
|
||||
|
||||
- Add option to slide up and down through controller
|
||||
|
||||
## [1.0.2]
|
||||
|
||||
- Make all callbacks type-safe
|
||||
|
|
|
|||
|
|
@ -110,6 +110,8 @@ The ```Controller``` is used to swipe the card from outside of the widget. You c
|
|||
| swipe | Changes the state of the controller to swipe and swipes the card in your selected direction.
|
||||
| swipeLeft | Changes the state of the controller to swipe left and swipes the card to the left side.
|
||||
| swipeRight | Changes the state of the controller to swipe right and swipes the card to the right side.
|
||||
| swipeTop | Changes the state of the controller to swipe top and swipes the card to the top side.
|
||||
| swipeBottom | Changes the state of the controller to swipe bottom and swipes the card to the | swipeBottom | Changes the state of the controller to swipe bottom and swipes the card to the right side.
|
||||
|
||||
<hr/>
|
||||
|
||||
|
|
|
|||
|
|
@ -57,6 +57,14 @@ class _ExamplePageState extends State<Example> {
|
|||
onPressed: controller.swipeRight,
|
||||
child: Icon(Icons.keyboard_arrow_right),
|
||||
),
|
||||
FloatingActionButton(
|
||||
onPressed: controller.swipeTop,
|
||||
child: Icon(Icons.keyboard_arrow_up),
|
||||
),
|
||||
FloatingActionButton(
|
||||
onPressed: controller.swipeBottom,
|
||||
child: Icon(Icons.keyboard_arrow_down),
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
|
|
|
|||
|
|
@ -92,49 +92,26 @@ class _CardSwiperState extends State<CardSwiper>
|
|||
super.initState();
|
||||
|
||||
if (widget.controller != null) {
|
||||
widget.controller!
|
||||
//swipe widget from the outside
|
||||
..addListener(() {
|
||||
if (widget.controller!.state == CardSwiperState.swipe) {
|
||||
if (widget.cards.isNotEmpty) {
|
||||
switch (widget.direction) {
|
||||
case CardSwiperDirection.right:
|
||||
_swipeHorizontal(context);
|
||||
widget.controller!.addListener(() {
|
||||
switch (widget.controller!.state) {
|
||||
case CardSwiperState.swipe:
|
||||
_swipe(context, widget.direction);
|
||||
break;
|
||||
case CardSwiperDirection.left:
|
||||
_swipeHorizontal(context);
|
||||
case CardSwiperState.swipeLeft:
|
||||
_swipe(context, CardSwiperDirection.left);
|
||||
break;
|
||||
case CardSwiperDirection.top:
|
||||
_swipeVertical(context);
|
||||
case CardSwiperState.swipeRight:
|
||||
_swipe(context, CardSwiperDirection.right);
|
||||
break;
|
||||
case CardSwiperDirection.bottom:
|
||||
_swipeVertical(context);
|
||||
case CardSwiperState.swipeTop:
|
||||
_swipe(context, CardSwiperDirection.top);
|
||||
break;
|
||||
case CardSwiperDirection.none:
|
||||
case CardSwiperState.swipeBottom:
|
||||
_swipe(context, CardSwiperDirection.bottom);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
_animationController.forward();
|
||||
}
|
||||
}
|
||||
})
|
||||
//swipe widget left from the outside
|
||||
..addListener(() {
|
||||
if (widget.controller!.state == CardSwiperState.swipeLeft) {
|
||||
if (widget.cards.isNotEmpty) {
|
||||
_left = -1;
|
||||
_swipeHorizontal(context);
|
||||
_animationController.forward();
|
||||
}
|
||||
}
|
||||
})
|
||||
//swipe widget right from the outside
|
||||
..addListener(() {
|
||||
if (widget.controller!.state == CardSwiperState.swipeRight) {
|
||||
if (widget.cards.isNotEmpty) {
|
||||
_left = widget.threshold + 1;
|
||||
_swipeHorizontal(context);
|
||||
_animationController.forward();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -340,6 +317,32 @@ class _CardSwiperState extends State<CardSwiper>
|
|||
}
|
||||
}
|
||||
|
||||
void _swipe(BuildContext context, CardSwiperDirection direction) {
|
||||
if (widget.cards.isEmpty) return;
|
||||
|
||||
switch (direction) {
|
||||
case CardSwiperDirection.left:
|
||||
_left = -1;
|
||||
_swipeHorizontal(context);
|
||||
break;
|
||||
case CardSwiperDirection.right:
|
||||
_left = widget.threshold + 1;
|
||||
_swipeHorizontal(context);
|
||||
break;
|
||||
case CardSwiperDirection.top:
|
||||
_top = -1;
|
||||
_swipeVertical(context);
|
||||
break;
|
||||
case CardSwiperDirection.bottom:
|
||||
_top = widget.threshold + 1;
|
||||
_swipeVertical(context);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
_animationController.forward();
|
||||
}
|
||||
|
||||
//moves the card away to the top or bottom
|
||||
void _swipeVertical(BuildContext context) {
|
||||
setState(() {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
//to call the swipe function from outside of the CardSwiper
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_card_swiper/src/enums.dart';
|
||||
|
||||
//to call the swipe function from outside of the CardSwiper
|
||||
class CardSwiperController extends ChangeNotifier {
|
||||
CardSwiperState? state;
|
||||
|
||||
|
|
@ -22,4 +22,16 @@ class CardSwiperController extends ChangeNotifier {
|
|||
state = CardSwiperState.swipeRight;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
//swipe the card to the top side by changing the status of the controller
|
||||
void swipeTop() {
|
||||
state = CardSwiperState.swipeTop;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
//swipe the card to the bottom side by changing the status of the controller
|
||||
void swipeBottom() {
|
||||
state = CardSwiperState.swipeBottom;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
enum CardSwiperState { swipe, swipeLeft, swipeRight }
|
||||
enum CardSwiperState { swipe, swipeLeft, swipeRight, swipeTop, swipeBottom }
|
||||
|
||||
enum CardSwiperDirection { none, left, right, top, bottom }
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue