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

Set property to enable or disable Gesture to resize crop area #194

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions example/lib/crop_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class CropPage extends StatelessWidget {
child: CropGridViewer.edit(
controller: controller,
rotateCropArea: false,
enableGestureResizeCrop: true,
margin: const EdgeInsets.symmetric(horizontal: 20),
),
),
Expand Down
53 changes: 35 additions & 18 deletions lib/src/widgets/crop/crop_grid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ class CropGridViewer extends StatefulWidget {
required this.controller,
}) : showGrid = false,
rotateCropArea = true,
enableGestureResizeCrop = false,
margin = EdgeInsets.zero;

const CropGridViewer.edit({
super.key,
required this.controller,
this.margin = const EdgeInsets.symmetric(horizontal: 20),
this.rotateCropArea = true,
this.enableGestureResizeCrop = true,
}) : showGrid = true;

/// The [controller] param is mandatory so every change in the controller settings will propagate in the crop view
Expand All @@ -54,6 +56,11 @@ class CropGridViewer extends StatefulWidget {
/// Defaults to `true` (like iOS Photos app crop)
final bool rotateCropArea;

/// The [enableGestureResizeCrop] parameters specifies if the crop area can be resized
///
/// Defaults to `true`
final bool enableGestureResizeCrop;

@override
State<CropGridViewer> createState() => _CropGridViewerState();
}
Expand Down Expand Up @@ -163,24 +170,26 @@ class _CropGridViewerState extends State<CropGridViewer> with CropPreviewMixin {
_boundary = CropBoundaries.inside;

// CORNERS
if (_expandedPosition(rect.value.topLeft).contains(pos)) {
_boundary = CropBoundaries.topLeft;
} else if (_expandedPosition(rect.value.topRight).contains(pos)) {
_boundary = CropBoundaries.topRight;
} else if (_expandedPosition(rect.value.bottomRight).contains(pos)) {
_boundary = CropBoundaries.bottomRight;
} else if (_expandedPosition(rect.value.bottomLeft).contains(pos)) {
_boundary = CropBoundaries.bottomLeft;
} else if (_controller.preferredCropAspectRatio == null) {
// CENTERS
if (_expandedPosition(rect.value.centerLeft).contains(pos)) {
_boundary = CropBoundaries.centerLeft;
} else if (_expandedPosition(rect.value.topCenter).contains(pos)) {
_boundary = CropBoundaries.topCenter;
} else if (_expandedPosition(rect.value.centerRight).contains(pos)) {
_boundary = CropBoundaries.centerRight;
} else if (_expandedPosition(rect.value.bottomCenter).contains(pos)) {
_boundary = CropBoundaries.bottomCenter;
if (widget.enableGestureResizeCrop) {
if (_expandedPosition(rect.value.topLeft).contains(pos)) {
_boundary = CropBoundaries.topLeft;
} else if (_expandedPosition(rect.value.topRight).contains(pos)) {
_boundary = CropBoundaries.topRight;
} else if (_expandedPosition(rect.value.bottomRight).contains(pos)) {
_boundary = CropBoundaries.bottomRight;
} else if (_expandedPosition(rect.value.bottomLeft).contains(pos)) {
_boundary = CropBoundaries.bottomLeft;
} else if (_controller.preferredCropAspectRatio == null) {
// CENTERS
if (_expandedPosition(rect.value.centerLeft).contains(pos)) {
_boundary = CropBoundaries.centerLeft;
} else if (_expandedPosition(rect.value.topCenter).contains(pos)) {
_boundary = CropBoundaries.topCenter;
} else if (_expandedPosition(rect.value.centerRight).contains(pos)) {
_boundary = CropBoundaries.centerRight;
} else if (_expandedPosition(rect.value.bottomCenter).contains(pos)) {
_boundary = CropBoundaries.bottomCenter;
}
}
}
setState(() {}); // to update selected boundary color
Expand All @@ -203,18 +212,26 @@ class _CropGridViewerState extends State<CropGridViewer> with CropPreviewMixin {
break;
//CORNERS
case CropBoundaries.topLeft:
if (!widget.enableGestureResizeCrop) break;

final Offset pos = rect.value.topLeft + delta;
_changeRect(left: pos.dx, top: pos.dy);
break;
case CropBoundaries.topRight:
if (!widget.enableGestureResizeCrop) break;

final Offset pos = rect.value.topRight + delta;
_changeRect(right: pos.dx, top: pos.dy);
break;
case CropBoundaries.bottomRight:
if (!widget.enableGestureResizeCrop) break;

final Offset pos = rect.value.bottomRight + delta;
_changeRect(right: pos.dx, bottom: pos.dy);
break;
case CropBoundaries.bottomLeft:
if (!widget.enableGestureResizeCrop) break;

final Offset pos = rect.value.bottomLeft + delta;
_changeRect(left: pos.dx, bottom: pos.dy);
break;
Expand Down