Skip to content

Commit

Permalink
fix: control image rect not to smaller than crop rect (chooyan-eng#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
chooyan-eng authored Mar 13, 2022
1 parent e88e0a0 commit 3f5a4f9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
1 change: 1 addition & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ class _CropSampleState extends State<CropSample> {
? const SizedBox.shrink()
: const DotControl(),
interactive: true,
fixArea: true,
),
Positioned(
right: 16,
Expand Down
35 changes: 26 additions & 9 deletions lib/src/crop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,20 @@ class _CropEditorState extends State<_CropEditor> {

void _updateScale(ScaleUpdateDetails detail) {
// move

var movedLeft = _imageRect.left + detail.focalPointDelta.dx;
if (movedLeft + _imageRect.width < _rect.right) {
movedLeft = _rect.right - _imageRect.width;
}

var movedTop = _imageRect.top + detail.focalPointDelta.dy;
if (movedTop + _imageRect.height < _rect.bottom) {
movedTop = _rect.bottom - _imageRect.height;
}
setState(() {
_imageRect = Rect.fromLTWH(
_imageRect.left + detail.focalPointDelta.dx,
_imageRect.top + detail.focalPointDelta.dy,
min(_rect.left, movedLeft),
min(_rect.top, movedTop),
_imageRect.width,
_imageRect.height,
);
Expand Down Expand Up @@ -253,10 +263,17 @@ class _CropEditorState extends State<_CropEditor> {
final newHeight = baseHeight * nextScale;
final topPositionDelta = (newHeight - _imageRect.height) * 0.5;

// position
final newLeft = max(min(_rect.left, _imageRect.left - leftPositionDelta),
_rect.right - newWidth);
final newTop = max(min(_rect.top, _imageRect.top - topPositionDelta),
_rect.bottom - newHeight);

if (newWidth < _rect.width || newHeight < _rect.height) {
return;
}
// apply
setState(() {
final newLeft = _imageRect.left - leftPositionDelta;
final newTop = _imageRect.top - topPositionDelta;
_imageRect = Rect.fromLTRB(
newLeft,
newTop,
Expand Down Expand Up @@ -335,11 +352,6 @@ class _CropEditorState extends State<_CropEditor> {

_imageRect = calculator.imageRect(screenSize, imageRatio);

if (widget.fixArea) {
final initialScale = calculator.scaleToCover(screenSize, _imageRect);
_applyScale(initialScale);
}

if (widget.initialAreaBuilder != null) {
rect = widget.initialAreaBuilder!(Rect.fromLTWH(
0,
Expand All @@ -350,6 +362,11 @@ class _CropEditorState extends State<_CropEditor> {
} else {
_resizeWith(widget.aspectRatio, widget.initialArea);
}

if (widget.interactive) {
final initialScale = calculator.scaleToCover(screenSize, _imageRect);
_applyScale(initialScale);
}
}

/// resize cropping area with given aspect ratio.
Expand Down

0 comments on commit 3f5a4f9

Please sign in to comment.