Skip to content

Commit

Permalink
fix(crop): correct issue with cropping for specific aspect ratios in …
Browse files Browse the repository at this point in the history
…embedded editor

- Resolved issue where cropping failed for specific aspect ratios when the editor was embedded.
- Updated relevant module in `crop_rotate_editor.dart`.
- Updated `CHANGELOG.md` and dependencies in `pubspec.yaml` for clarity.
  • Loading branch information
hm21 committed Jan 3, 2025
1 parent 22a21a2 commit 78d16c9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 7.3.1
- **FIX**(crop): Corrected an issue where cropping with specific aspect ratios did not work when the editor was embedded.

## 7.3.0
- **FEAT**(dependencies): Update `emoji_picker_flutter` to `4.2.0` and `image` to `4.5.2`. Adds support for custom translations in emoji editor for emoji search.

Expand Down
42 changes: 39 additions & 3 deletions lib/modules/crop_rotate_editor/crop_rotate_editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ class CropRotateEditorState extends State<CropRotateEditor>
StandaloneEditorState<CropRotateEditor, CropRotateEditorInitConfigs>,
ExtendedLoop,
CropAreaHistory {
/// A global key used to identify the editor content widget.
final _editorContentKey = GlobalKey();

/// An offset helper to keep track of the editor's screen offset.
/// This is required for the case the editor is embedded inside the screen.
/// Initialized to `Offset.zero`.
Offset _editorScreenOffsetHelper = Offset.zero;

final _mouseCursorsKey = GlobalKey<ExtendedMouseRegionState>();

/// A key used to access the state of the CropRotateGestureDetector widget.
Expand Down Expand Up @@ -1224,6 +1232,8 @@ class CropRotateEditorState extends State<CropRotateEditor>
if (_blockInteraction || details.pointerCount > 2) return;
_blockInteraction = true;

_editorScreenOffsetHelper = _calculateEditorScreenOffset();

_startingPinchScale = userScaleFactor;
_startingTranslate = translate;
// Calculate the center offset point from the old zoomed view
Expand Down Expand Up @@ -1259,6 +1269,24 @@ class CropRotateEditorState extends State<CropRotateEditor>
_blockInteraction = false;
}

/// Calculates the offset of the editor screen.
///
/// This method determines the position of the editor content on the screen
/// by converting the local coordinates of the render box to global
/// coordinates.
///
/// Returns an [Offset] representing the position of the editor content.
/// If the editor content context is null, it returns [Offset.zero].
Offset _calculateEditorScreenOffset() {
if (_editorContentKey.currentContext == null) return Offset.zero;

final RenderBox renderBox =
_editorContentKey.currentContext!.findRenderObject() as RenderBox;
final Offset position = renderBox.localToGlobal(Offset.zero);

return position;
}

void _onScaleUpdate(ScaleUpdateDetails details) {
if (_blockInteraction ||
details.pointerCount > 2 ||
Expand All @@ -1285,8 +1313,11 @@ class CropRotateEditorState extends State<CropRotateEditor>
if (_currentCropAreaPart != CropAreaPart.none &&
_currentCropAreaPart != CropAreaPart.inside) {
Offset offset = _getRealHitPoint(
zoom: _startingPinchScale, position: details.localFocalPoint) +
zoom: _startingPinchScale,
position: details.localFocalPoint,
) +
_startingTranslate * _startingPinchScale;

bool roundCropper = cropRotateEditorConfigs.roundCropper;

double imgW = _renderedImgConstraints.maxWidth;
Expand Down Expand Up @@ -1371,12 +1402,16 @@ class CropRotateEditorState extends State<CropRotateEditor>
doubleInteractiveArea);

double outsideHitPosY = details.focalPoint.dy -
_editorScreenOffsetHelper.dy -
(_hasToolbar ? kToolbarHeight : 0) -
MediaQuery.of(context).padding.top;

bool outsideLeft = details.focalPoint.dx < zoomOutHitAreaX;
bool outsideLeft =
details.focalPoint.dx - _editorScreenOffsetHelper.dx <
zoomOutHitAreaX;
bool outsideRight =
details.focalPoint.dx > editorBodySize.width - zoomOutHitAreaX;
details.focalPoint.dx - _editorScreenOffsetHelper.dx >
editorBodySize.width - zoomOutHitAreaX;
bool outsideTop = outsideHitPosY < zoomOutHitAreaY;
bool outsideBottom =
outsideHitPosY > editorBodySize.height - zoomOutHitAreaY;
Expand Down Expand Up @@ -1929,6 +1964,7 @@ class CropRotateEditorState extends State<CropRotateEditor>
@override
Widget build(BuildContext context) {
return SafeArea(
key: _editorContentKey,
top: cropRotateEditorConfigs.safeArea.top,
bottom: cropRotateEditorConfigs.safeArea.bottom,
left: cropRotateEditorConfigs.safeArea.left,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: pro_image_editor
description: "A Flutter image editor: Seamlessly enhance your images with user-friendly editing features."
version: 7.3.0
version: 7.3.1
homepage: https://github.com/hm21/pro_image_editor/
repository: https://github.com/hm21/pro_image_editor/
issue_tracker: https://github.com/hm21/pro_image_editor/issues/
Expand Down

0 comments on commit 78d16c9

Please sign in to comment.