Skip to content

Commit

Permalink
feat: update controller to use stream
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardodalarme committed Jan 28, 2024
1 parent 7b79e30 commit a79449d
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 403 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
## [7.0.0]

- **BREAKING CHANGE**:
- Upgrade min dart sdk to 3.0.0
- Upgrade min dart sdk to 3.0.0
- Replace `swipe`, `swipeLeft`, `swipeRight`, `swipeUp`, `swipeDown` with `swipe(CardSwiperDirection direction)`
- It also removes `direction` from the `CardSwiper` widget

## [6.1.0]

Expand Down
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,15 @@ class Example extends StatelessWidget {
| padding | EdgeInsets.symmetric(horizontal: 20, vertical: 25) | The padding around the swiper | false |
| scale | 0.9 | Scale of the card that is behind the front card | false |
| threshold | 50 | Threshold from which the card is swiped away | false |
| onSwipeDirectionChange | - | A callback containing the horizontal and vertical swipe direction | false |
| onSwipeDirectionChange | - | A callback containing the horizontal and vertical swipe direction
| false |
#### Controller

The `Controller` is used to swipe the card from outside of the widget. You can create a controller called `CardSwiperController` and save the instance for further usage. Please have a closer look at our [Example](https://github.com/ricardodalarme/flutter_card_swiper/tree/main/example) for the usage.

| Method | Description |
| ----------- | :--------------------------------------------- |
| swipe | Swipes the card in the selected direction. |
| swipeLeft | Swipes the card to the left side. |
| swipeRight | Swipes the card to the right side. |
| swipeTop | Swipes the card to the top side. |
| swipeBottom | Swipes the card to the bottom side. |
| swipe | Swipes the card to a specific direction. |
| undo | Bring back the last card that was swiped away. |

<hr/>
Expand Down
10 changes: 6 additions & 4 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,21 @@ class _ExamplePageState extends State<Example> {
child: const Icon(Icons.rotate_left),
),
FloatingActionButton(
onPressed: controller.swipeLeft,
onPressed: () => controller.swipe(CardSwiperDirection.left),
child: const Icon(Icons.keyboard_arrow_left),
),
FloatingActionButton(
onPressed: controller.swipeRight,
onPressed: () =>
controller.swipe(CardSwiperDirection.right),
child: const Icon(Icons.keyboard_arrow_right),
),
FloatingActionButton(
onPressed: controller.swipeTop,
onPressed: () => controller.swipe(CardSwiperDirection.top),
child: const Icon(Icons.keyboard_arrow_up),
),
FloatingActionButton(
onPressed: controller.swipeBottom,
onPressed: () =>
controller.swipe(CardSwiperDirection.bottom),
child: const Icon(Icons.keyboard_arrow_down),
),
],
Expand Down
2 changes: 1 addition & 1 deletion lib/flutter_card_swiper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// animations supporting Android, iOS, Web & Desktop.
library flutter_card_swiper;

export 'package:flutter_card_swiper/src/card_swiper_controller.dart';
export 'package:flutter_card_swiper/src/controller/card_swiper_controller.dart';
export 'package:flutter_card_swiper/src/enums.dart';
export 'package:flutter_card_swiper/src/properties/allowed_swipe_direction.dart';
export 'package:flutter_card_swiper/src/typedefs.dart';
Expand Down
43 changes: 0 additions & 43 deletions lib/src/card_swiper_controller.dart

This file was deleted.

26 changes: 26 additions & 0 deletions lib/src/controller/card_swiper_controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'dart:async';

import 'package:flutter_card_swiper/src/controller/controller_event.dart';
import 'package:flutter_card_swiper/src/enums.dart';

/// A controller that can be used to trigger swipes on a CardSwiper widget.
class CardSwiperController {
final _eventController = StreamController<ControllerEvent>.broadcast();

/// Stream of events that can be used to swipe the card.
Stream<ControllerEvent> get events => _eventController.stream;

/// Swipe the card to a specific direction.
void swipe(CardSwiperDirection direction) {
_eventController.add(ControllerSwipeEvent(direction));
}

// Undo the last swipe
void undo() {
_eventController.add(const ControllerUndoEvent());
}

Future<void> dispose() async {
await _eventController.close();
}
}
15 changes: 15 additions & 0 deletions lib/src/controller/controller_event.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:flutter_card_swiper/flutter_card_swiper.dart';

abstract class ControllerEvent {
const ControllerEvent();
}

class ControllerSwipeEvent extends ControllerEvent {
final CardSwiperDirection direction;

const ControllerSwipeEvent(this.direction);
}

class ControllerUndoEvent extends ControllerEvent {
const ControllerUndoEvent();
}
9 changes: 0 additions & 9 deletions lib/src/enums.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
enum CardSwiperState {
swipe,
swipeLeft,
swipeRight,
swipeTop,
swipeBottom,
undo
}

enum CardSwiperDirection { none, left, right, top, bottom }

enum SwipeType { none, swipe, back, undo }
13 changes: 2 additions & 11 deletions lib/src/widget/card_swiper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import 'dart:math' as math;

import 'package:flutter/widgets.dart';
import 'package:flutter_card_swiper/src/card_animation.dart';
import 'package:flutter_card_swiper/src/card_swiper_controller.dart';
import 'package:flutter_card_swiper/src/controller/card_swiper_controller.dart';
import 'package:flutter_card_swiper/src/controller/controller_event.dart';
import 'package:flutter_card_swiper/src/enums.dart';
import 'package:flutter_card_swiper/src/properties/allowed_swipe_direction.dart';
import 'package:flutter_card_swiper/src/typedefs.dart';
Expand Down Expand Up @@ -89,11 +90,6 @@ class CardSwiper extends StatefulWidget {
/// Callback function that is called when the swiper is disabled.
final CardSwiperOnTapDisabled? onTapDisabled;

/// The direction in which the card is swiped when triggered by the [controller].
///
/// Defaults to [CardSwiperDirection.right].
final CardSwiperDirection direction;

/// Defined the directions in which the card is allowed to be swiped.
/// Defaults to [AllowedSwipeDirection.all]
final AllowedSwipeDirection allowedSwipeDirection;
Expand Down Expand Up @@ -142,7 +138,6 @@ class CardSwiper extends StatefulWidget {
this.onTapDisabled,
this.onSwipe,
this.onEnd,
this.direction = CardSwiperDirection.right,
this.onSwipeDirectionChange,
this.allowedSwipeDirection = const AllowedSwipeDirection.all(),
this.isLoop = true,
Expand All @@ -158,10 +153,6 @@ class CardSwiper extends StatefulWidget {
threshold >= 1 && threshold <= 100,
'threshold must be between 1 and 100',
),
assert(
direction != CardSwiperDirection.none,
'direction must not be none',
),
assert(
scale >= 0 && scale <= 1,
'scale must be between 0 and 1',
Expand Down
29 changes: 9 additions & 20 deletions lib/src/widget/card_swiper_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper>

_undoableIndex.state = widget.initialIndex;

widget.controller?.addListener(_controllerListener);
widget.controller?.events.listen(_controllerListener);

_animationController = AnimationController(
duration: widget.duration,
Expand Down Expand Up @@ -65,7 +65,7 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper>
@override
void dispose() {
_animationController.dispose();
widget.controller?.removeListener(_controllerListener);
widget.controller?.dispose();
super.dispose();
}

Expand Down Expand Up @@ -157,23 +157,12 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper>
);
}

void _controllerListener() {
switch (widget.controller?.state) {
case CardSwiperState.swipe:
return _swipe(widget.direction);
case CardSwiperState.swipeLeft:
return _swipe(CardSwiperDirection.left);
case CardSwiperState.swipeRight:
return _swipe(CardSwiperDirection.right);
case CardSwiperState.swipeTop:
return _swipe(CardSwiperDirection.top);
case CardSwiperState.swipeBottom:
return _swipe(CardSwiperDirection.bottom);
case CardSwiperState.undo:
return _undo();
default:
return;
}
void _controllerListener(ControllerEvent event) {
return switch (event) {
ControllerSwipeEvent(:final direction) => _swipe(direction),
ControllerUndoEvent() => _undo(),
_ => null
};
}

void _animationListener() {
Expand Down Expand Up @@ -225,7 +214,7 @@ class _CardSwiperState<T extends Widget> extends State<CardSwiper>

void _onEndAnimation() {
final direction = _getEndAnimationDirection();
final isValidDirection = this._isValidDirection(direction);
final isValidDirection = _isValidDirection(direction);

if (isValidDirection) {
_swipe(direction);
Expand Down
Loading

0 comments on commit a79449d

Please sign in to comment.