diff --git a/CHANGELOG.md b/CHANGELOG.md index c14c872..e771a0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## [1.1.0] + +- Add option to slide up and down through controller + ## [1.0.2] - Make all callbacks type-safe diff --git a/README.md b/README.md index 2dddba5..1f08328 100644 --- a/README.md +++ b/README.md @@ -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.
diff --git a/example/lib/main.dart b/example/lib/main.dart index 2c97e95..4cb9ea9 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -57,6 +57,14 @@ class _ExamplePageState extends State { 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), + ), ], ), ) diff --git a/lib/src/card_swiper.dart b/lib/src/card_swiper.dart index 6b8f946..d444d88 100644 --- a/lib/src/card_swiper.dart +++ b/lib/src/card_swiper.dart @@ -92,51 +92,28 @@ class _CardSwiperState extends State 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); - break; - case CardSwiperDirection.left: - _swipeHorizontal(context); - break; - case CardSwiperDirection.top: - _swipeVertical(context); - break; - case CardSwiperDirection.bottom: - _swipeVertical(context); - break; - case CardSwiperDirection.none: - 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(); - } - } - }); + //swipe widget from the outside + widget.controller!.addListener(() { + switch (widget.controller!.state) { + case CardSwiperState.swipe: + _swipe(context, widget.direction); + break; + case CardSwiperState.swipeLeft: + _swipe(context, CardSwiperDirection.left); + break; + case CardSwiperState.swipeRight: + _swipe(context, CardSwiperDirection.right); + break; + case CardSwiperState.swipeTop: + _swipe(context, CardSwiperDirection.top); + break; + case CardSwiperState.swipeBottom: + _swipe(context, CardSwiperDirection.bottom); + break; + default: + break; + } + }); } if (widget.maxAngle > 0) { @@ -340,6 +317,32 @@ class _CardSwiperState extends State } } + 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(() { diff --git a/lib/src/card_swiper_controller.dart b/lib/src/card_swiper_controller.dart index 6b6389d..b6828a5 100644 --- a/lib/src/card_swiper_controller.dart +++ b/lib/src/card_swiper_controller.dart @@ -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(); + } } diff --git a/lib/src/enums.dart b/lib/src/enums.dart index 6db9c11..8f0b86c 100644 --- a/lib/src/enums.dart +++ b/lib/src/enums.dart @@ -1,4 +1,4 @@ -enum CardSwiperState { swipe, swipeLeft, swipeRight } +enum CardSwiperState { swipe, swipeLeft, swipeRight, swipeTop, swipeBottom } enum CardSwiperDirection { none, left, right, top, bottom }