feat(controller): allow swiping up and down

This commit is contained in:
Ricardo Dalarme 2023-01-24 15:25:39 -03:00
parent c8f044cc7d
commit 06ac501f9d
6 changed files with 76 additions and 47 deletions

View File

@ -1,3 +1,7 @@
## [1.1.0]
- Add option to slide up and down through controller
## [1.0.2] ## [1.0.2]
- Make all callbacks type-safe - Make all callbacks type-safe

View File

@ -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. | 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. | 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. | 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/> <hr/>

View File

@ -57,6 +57,14 @@ class _ExamplePageState extends State<Example> {
onPressed: controller.swipeRight, onPressed: controller.swipeRight,
child: Icon(Icons.keyboard_arrow_right), 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),
),
], ],
), ),
) )

View File

@ -92,49 +92,26 @@ class _CardSwiperState extends State<CardSwiper>
super.initState(); super.initState();
if (widget.controller != null) { if (widget.controller != null) {
widget.controller!
//swipe widget from the outside //swipe widget from the outside
..addListener(() { widget.controller!.addListener(() {
if (widget.controller!.state == CardSwiperState.swipe) { switch (widget.controller!.state) {
if (widget.cards.isNotEmpty) { case CardSwiperState.swipe:
switch (widget.direction) { _swipe(context, widget.direction);
case CardSwiperDirection.right:
_swipeHorizontal(context);
break; break;
case CardSwiperDirection.left: case CardSwiperState.swipeLeft:
_swipeHorizontal(context); _swipe(context, CardSwiperDirection.left);
break; break;
case CardSwiperDirection.top: case CardSwiperState.swipeRight:
_swipeVertical(context); _swipe(context, CardSwiperDirection.right);
break; break;
case CardSwiperDirection.bottom: case CardSwiperState.swipeTop:
_swipeVertical(context); _swipe(context, CardSwiperDirection.top);
break; break;
case CardSwiperDirection.none: case CardSwiperState.swipeBottom:
_swipe(context, CardSwiperDirection.bottom);
break;
default:
break; 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 //moves the card away to the top or bottom
void _swipeVertical(BuildContext context) { void _swipeVertical(BuildContext context) {
setState(() { setState(() {

View File

@ -1,7 +1,7 @@
//to call the swipe function from outside of the CardSwiper
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:flutter_card_swiper/src/enums.dart'; import 'package:flutter_card_swiper/src/enums.dart';
//to call the swipe function from outside of the CardSwiper
class CardSwiperController extends ChangeNotifier { class CardSwiperController extends ChangeNotifier {
CardSwiperState? state; CardSwiperState? state;
@ -22,4 +22,16 @@ class CardSwiperController extends ChangeNotifier {
state = CardSwiperState.swipeRight; state = CardSwiperState.swipeRight;
notifyListeners(); 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();
}
} }

View File

@ -1,4 +1,4 @@
enum CardSwiperState { swipe, swipeLeft, swipeRight } enum CardSwiperState { swipe, swipeLeft, swipeRight, swipeTop, swipeBottom }
enum CardSwiperDirection { none, left, right, top, bottom } enum CardSwiperDirection { none, left, right, top, bottom }