Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow swipe to custom angles #46

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## [7.0.2]

- Added `CardAnimation.animateToAngle` helper to animate swipe the card to any given angle between 0-360°.
- Added `CardAnimation.animateUndoFromAngle` helper method to undo animation from any angle.
- Remoed previous implementations for the above in the `CardAnimation` class, namely - `animateHorizontally`, `animateVertically`, `animateUndoHorizontally`, and `animateUndoVertically`
- Replaced `enum CardSwiperDirection` with `class CardSwiperDirection` to support custom angle swiping.

## [7.0.1]

- Prevents `CardSwiperController` to be disposed by `CardSwiper`.
Expand Down
1 change: 1 addition & 0 deletions lib/flutter_card_swiper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
library flutter_card_swiper;

export 'package:flutter_card_swiper/src/controller/card_swiper_controller.dart';
export 'package:flutter_card_swiper/src/direction/card_swiper_direction.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
92 changes: 30 additions & 62 deletions lib/src/card_animation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,56 +136,41 @@ class CardAnimation {
}

void animate(BuildContext context, CardSwiperDirection direction) {
return switch (direction) {
CardSwiperDirection.left => animateHorizontally(context, false),
CardSwiperDirection.right => animateHorizontally(context, true),
CardSwiperDirection.top => animateVertically(context, false),
CardSwiperDirection.bottom => animateVertically(context, true),
CardSwiperDirection.none => null,
};
if (direction == CardSwiperDirection.none) return;
animateToAngle(context, direction.angle);
}

void animateHorizontally(BuildContext context, bool isToRight) {
final screenWidth = MediaQuery.of(context).size.width;
void animateToAngle(BuildContext context, double targetAngle) {
final size = MediaQuery.of(context).size;

_leftAnimation = Tween<double>(
begin: left,
end: isToRight ? screenWidth : -screenWidth,
).animate(animationController);
_topAnimation = Tween<double>(
begin: top,
end: top + top,
).animate(animationController);
_scaleAnimation = Tween<double>(
begin: scale,
end: 1.0,
).animate(animationController);
_differenceAnimation = Tween<Offset>(
begin: difference,
end: initialOffset,
).animate(animationController);
animationController.forward();
}
// Convert the angle to radians
final adjustedAngle = (targetAngle - 90) * (math.pi / 180);

void animateVertically(BuildContext context, bool isToBottom) {
final screenHeight = MediaQuery.of(context).size.height;
// Calculate the target position based on the angle
final magnitude = size.width; // Use screen width as base magnitude
final targetX = magnitude * math.cos(adjustedAngle);
final targetY = magnitude * math.sin(adjustedAngle);

_leftAnimation = Tween<double>(
begin: left,
end: left + left,
end: targetX,
).animate(animationController);

_topAnimation = Tween<double>(
begin: top,
end: isToBottom ? screenHeight : -screenHeight,
end: targetY,
).animate(animationController);

_scaleAnimation = Tween<double>(
begin: scale,
end: 1.0,
).animate(animationController);

_differenceAnimation = Tween<Offset>(
begin: difference,
end: initialOffset,
).animate(animationController);

animationController.forward();
}

Expand All @@ -210,56 +195,39 @@ class CardAnimation {
}

void animateUndo(BuildContext context, CardSwiperDirection direction) {
return switch (direction) {
CardSwiperDirection.left => animateUndoHorizontally(context, false),
CardSwiperDirection.right => animateUndoHorizontally(context, true),
CardSwiperDirection.top => animateUndoVertically(context, false),
CardSwiperDirection.bottom => animateUndoVertically(context, true),
_ => null
};
if (direction == CardSwiperDirection.none) return;
animateUndoFromAngle(context, direction.angle);
}

void animateUndoHorizontally(BuildContext context, bool isToRight) {
void animateUndoFromAngle(BuildContext context, double angle) {
final size = MediaQuery.of(context).size;

_leftAnimation = Tween<double>(
begin: isToRight ? size.width : -size.width,
end: 0,
).animate(animationController);
_topAnimation = Tween<double>(
begin: top,
end: top + top,
).animate(animationController);
_scaleAnimation = Tween<double>(
begin: 1.0,
end: scale,
).animate(animationController);
_differenceAnimation = Tween<Offset>(
begin: initialOffset,
end: difference,
).animate(animationController);
animationController.forward();
}
final adjustedAngle = (angle - 90) * (math.pi / 180);

void animateUndoVertically(BuildContext context, bool isToBottom) {
final size = MediaQuery.of(context).size;
final magnitude = size.width;
final startX = magnitude * math.cos(adjustedAngle);
final startY = magnitude * math.sin(adjustedAngle);

_leftAnimation = Tween<double>(
begin: left,
end: left + left,
begin: startX,
end: 0,
).animate(animationController);

_topAnimation = Tween<double>(
begin: isToBottom ? -size.height : size.height,
begin: startY,
end: 0,
).animate(animationController);

_scaleAnimation = Tween<double>(
begin: 1.0,
end: scale,
).animate(animationController);

_differenceAnimation = Tween<Offset>(
begin: initialOffset,
end: difference,
).animate(animationController);

animationController.forward();
}
}
2 changes: 1 addition & 1 deletion lib/src/controller/card_swiper_controller.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:async';

import 'package:flutter_card_swiper/flutter_card_swiper.dart';
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 {
Expand Down
116 changes: 116 additions & 0 deletions lib/src/direction/card_swiper_direction.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/// Represents the direction of a card swipe using an angle.
///
/// The direction is represented by an angle in degrees, following a clockwise rotation:
/// * 0° points to the top
/// * 90° points to the right
/// * 180° points to the bottom
/// * 270° points to the left
///
/// The class provides standard cardinal directions as static constants:
/// ```dart
/// CardSwiperDirection.top // 0°
/// CardSwiperDirection.right // 90°
/// CardSwiperDirection.bottom // 180°
/// CardSwiperDirection.left // 270°
/// ```
///
/// Custom angles can be created using [CardSwiperDirection.custom]:
/// ```dart
/// final diagonal = CardSwiperDirection.custom(45); // Creates a top-right direction
/// ```
///
/// All angles are normalized to be within the range [0, 360) degrees. When comparing
/// directions, a tolerance of 5 degrees is used by default to account for small variations
/// in swipe gestures.
///
/// The direction also maintains a human-readable name, which is automatically generated
/// based on the angle's quadrant (e.g., 'top-right', 'right-bottom') or can be
/// manually specified when creating a custom direction.
class CardSwiperDirection {
/// The angle in degrees representing the direction of the swipe
final double angle;

/// The name of the direction.
///
/// This is not used in any operations - can be considered as a debug info if you may.
final String name;

/// Creates a new [CardSwiperDirection] with the specified angle in degrees
const CardSwiperDirection._({
required this.angle,
required this.name,
});

/// No movement direction (Infinity)
static const none = CardSwiperDirection._(
angle: double.infinity,
name: 'none',
);

/// Swipe to the top (0 degrees)
static const top = CardSwiperDirection._(angle: 0, name: 'top');

/// Swipe to the right (90 degrees)
static const right = CardSwiperDirection._(angle: 90, name: 'right');

/// Swipe to the bottom (180 degrees)
static const bottom = CardSwiperDirection._(angle: 180, name: 'bottom');

/// Swipe to the left (270 degrees)
static const left = CardSwiperDirection._(angle: 270, name: 'left');

/// Creates a custom swipe direction with the specified angle in degrees
factory CardSwiperDirection.custom(double angle, {String? name}) {
// Normalize angle to be between 0 and 360 degrees
final normalizedAngle = (angle % 360 + 360) % 360;
// Generate a name if not provided
final directionName = name ?? _getDirectionName(normalizedAngle);
return CardSwiperDirection._(
angle: normalizedAngle,
name: directionName,
);
}

/// Generate a direction name based on the angle
static String _getDirectionName(double angle) {
if (angle == 0) return 'top';
if (angle == 90) return 'right';
if (angle == 180) return 'bottom';
if (angle == 270) return 'left';

// For custom angles, generate a name based on the quadrant
if (angle > 0 && angle < 90) return 'top-right';
if (angle > 90 && angle < 180) return 'right-bottom';
if (angle > 180 && angle < 270) return 'bottom-left';
return 'left-top';
}

/// Checks if this direction is approximately equal to another direction
/// within a certain tolerance (default is 5 degrees)
bool isCloseTo(CardSwiperDirection other, {double tolerance = 5}) {
final diff = (angle - other.angle).abs();
return diff <= tolerance || (360 - diff) <= tolerance;
}

/// Returns true if the direction is horizontal (left or right)
bool get isHorizontal => isCloseTo(right) || isCloseTo(left);

/// Returns true if the direction is vertical (top or bottom)
bool get isVertical => isCloseTo(top) || isCloseTo(bottom);

@override
// ignore: avoid_equals_and_hash_code_on_mutable_classes
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is CardSwiperDirection &&
other.angle == angle &&
other.name == name;
}

@override
// ignore: avoid_equals_and_hash_code_on_mutable_classes
int get hashCode => Object.hash(angle, name);

@override
String toString() => 'CardSwiperDirection($name: $angle°)';
}
2 changes: 0 additions & 2 deletions lib/src/enums.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
enum CardSwiperDirection { none, left, right, top, bottom }

enum SwipeType { none, swipe, back, undo }
2 changes: 1 addition & 1 deletion lib/src/typedefs.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:async';

import 'package:flutter/widgets.dart';
import 'package:flutter_card_swiper/src/enums.dart';
import 'package:flutter_card_swiper/src/direction/card_swiper_direction.dart';

typedef CardSwiperOnSwipe = FutureOr<bool> Function(
int previousIndex,
Expand Down
26 changes: 18 additions & 8 deletions lib/src/utils/direction_extension.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_card_swiper/src/enums.dart';
import 'package:flutter_card_swiper/flutter_card_swiper.dart';

extension DirectionExtension on CardSwiperDirection {
Axis get axis => switch (this) {
CardSwiperDirection.left ||
CardSwiperDirection.right =>
Axis.horizontal,
CardSwiperDirection.top || CardSwiperDirection.bottom => Axis.vertical,
CardSwiperDirection.none => throw Exception('Direction is none'),
};
Axis get axis {
if (this == CardSwiperDirection.left || this == CardSwiperDirection.right) {
return Axis.horizontal;
} else if (this == CardSwiperDirection.top ||
this == CardSwiperDirection.bottom) {
return Axis.vertical;
} else if (this == CardSwiperDirection.none) {
throw Exception('Direction is none');
} else {
// Handle custom angles: if the angle is closer to horizontal or vertical
if ((angle >= 45 && angle <= 135) || (angle >= 225 && angle <= 315)) {
return Axis.vertical; // Top/Bottom-ish
} else {
return Axis.horizontal; // Left/Right-ish
}
}
}
}
5 changes: 1 addition & 4 deletions lib/src/widget/card_swiper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ import 'dart:collection';
import 'dart:math' as math;

import 'package:flutter/widgets.dart';
import 'package:flutter_card_swiper/flutter_card_swiper.dart';
import 'package:flutter_card_swiper/src/card_animation.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';
import 'package:flutter_card_swiper/src/utils/number_extension.dart';
import 'package:flutter_card_swiper/src/utils/undoable.dart';

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: flutter_card_swiper
description: This is a Tinder-like card swiper package. It allows you to swipe left, right, up, and down and define your own business logic for each direction.
homepage: https://github.com/ricardodalarme/flutter_card_swiper
issue_tracker: https://github.com/ricardodalarme/flutter_card_swiper/issues
version: 7.0.1
version: 7.0.2

environment:
sdk: ">=3.0.0 <4.0.0"
Expand Down
2 changes: 1 addition & 1 deletion test/utils/direction_extension_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_card_swiper/src/enums.dart';
import 'package:flutter_card_swiper/flutter_card_swiper.dart';
import 'package:flutter_card_swiper/src/utils/direction_extension.dart';
import 'package:flutter_test/flutter_test.dart';

Expand Down