Skip to content

Commit 83dbed4

Browse files
committed
format dart file
1 parent dd11f50 commit 83dbed4

File tree

2 files changed

+57
-28
lines changed

2 files changed

+57
-28
lines changed

lib/src/calculators/calculate_crop_fit_params.dart

+4-2
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ CropFitParams calculateCropFitParams({
2727
if (screenWidth <= screenHeight * aspectRatio) {
2828
cropSizeWidth = screenWidth * cropPercentage;
2929
cropSizeHeight = cropSizeWidth / aspectRatio;
30-
defaultScale = cropSizeWidth * max(imageWidth / imageHeight, 1.0) / imageWidth;
30+
defaultScale =
31+
cropSizeWidth * max(imageWidth / imageHeight, 1.0) / imageWidth;
3132
} else {
3233
cropSizeHeight = screenHeight * cropPercentage;
3334
cropSizeWidth = cropSizeHeight * aspectRatio;
34-
defaultScale = cropSizeHeight * max(imageHeight / imageWidth, 1.0) / imageHeight;
35+
defaultScale =
36+
cropSizeHeight * max(imageHeight / imageWidth, 1.0) / imageHeight;
3537
}
3638
break;
3739

lib/src/widgets/custom_image_crop_widget.dart

+53-26
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ class CustomImageCrop extends StatefulWidget {
133133
Paint? imagePaintDuringCrop,
134134
this.forceInsideCropArea = false,
135135
Key? key,
136-
}) : this.imagePaintDuringCrop =
137-
imagePaintDuringCrop ?? (Paint()..filterQuality = FilterQuality.high),
136+
}) : this.imagePaintDuringCrop = imagePaintDuringCrop ??
137+
(Paint()..filterQuality = FilterQuality.high),
138138
assert(
139139
!(shape == CustomCropShape.Ratio && ratio == null),
140140
"If shape is set to Ratio, ratio should not be null.",
@@ -301,7 +301,8 @@ class _CustomImageCropState extends State<CustomImageCrop>
301301
void onMoveUpdate(MoveEvent event) {
302302
if (!widget.canMove) return;
303303

304-
widget.cropController.addTransition(CropImageData(x: event.delta.dx, y: event.delta.dy));
304+
widget.cropController
305+
.addTransition(CropImageData(x: event.delta.dx, y: event.delta.dy));
305306
}
306307

307308
Rect _getInitialImageRect() {
@@ -317,7 +318,8 @@ class _CustomImageCropState extends State<CustomImageCrop>
317318
aspectRatio: (widget.ratio?.width ?? 1) / (widget.ratio?.height ?? 1),
318319
);
319320
final initialWidth = _imageAsUIImage!.width * cropFitParams.additionalScale;
320-
final initialHeight = _imageAsUIImage!.height * cropFitParams.additionalScale;
321+
final initialHeight =
322+
_imageAsUIImage!.height * cropFitParams.additionalScale;
321323
return Rect.fromLTWH(
322324
(_width - initialWidth) / 2,
323325
(_height - initialHeight) / 2,
@@ -346,13 +348,18 @@ class _CustomImageCropState extends State<CustomImageCrop>
346348

347349
if (transition.x != 0 || transition.y != 0) {
348350
if (isRotated) {
349-
_addTransitionInternal(CropImageData(x: startX - data.x, y: startY - data.y));
351+
_addTransitionInternal(
352+
CropImageData(x: startX - data.x, y: startY - data.y));
350353
} else {
351354
final imageRect = _getImageRect(initialImageRect, data.scale);
352355
double deltaX = min(pathRect.left - imageRect.left, 0);
353-
deltaX = pathRect.right > imageRect.right ? pathRect.right - imageRect.right : deltaX;
356+
deltaX = pathRect.right > imageRect.right
357+
? pathRect.right - imageRect.right
358+
: deltaX;
354359
double deltaY = min(pathRect.top - imageRect.top, 0);
355-
deltaY = pathRect.bottom > imageRect.bottom ? pathRect.bottom - imageRect.bottom : deltaY;
360+
deltaY = pathRect.bottom > imageRect.bottom
361+
? pathRect.bottom - imageRect.bottom
362+
: deltaY;
356363
_addTransitionInternal(CropImageData(x: deltaX, y: deltaY));
357364
}
358365
return;
@@ -361,22 +368,31 @@ class _CustomImageCropState extends State<CustomImageCrop>
361368
_addTransitionInternal(CropImageData(scale: startScale / data.scale));
362369
return;
363370
}
364-
double minEdgeHalf = min(initialImageRect.width, initialImageRect.height) / 2;
365-
double adaptScale = _calculateScaleAfterRotate(pathRect, startScale, initialImageRect, minEdgeHalf);
371+
double minEdgeHalf =
372+
min(initialImageRect.width, initialImageRect.height) / 2;
373+
double adaptScale = _calculateScaleAfterRotate(
374+
pathRect, startScale, initialImageRect, minEdgeHalf);
366375
_addTransitionInternal(CropImageData(scale: adaptScale / data.scale));
367376
}
368377

369378
Rect _getImageRect(Rect initialImageRect, double currentScale) {
370379
final diffScale = (1 - currentScale) / 2;
371-
final left = initialImageRect.left + diffScale * initialImageRect.width + data.x;
372-
final top = initialImageRect.top + diffScale * initialImageRect.height + data.y;
380+
final left =
381+
initialImageRect.left + diffScale * initialImageRect.width + data.x;
382+
final top =
383+
initialImageRect.top + diffScale * initialImageRect.height + data.y;
373384
Rect imageRect = Rect.fromLTWH(
374-
left, top, currentScale * initialImageRect.width, currentScale * initialImageRect.height);
385+
left,
386+
top,
387+
currentScale * initialImageRect.width,
388+
currentScale * initialImageRect.height);
375389
return imageRect;
376390
}
377391

378-
double _getDistanceBetweenPointAndLine(Offset point, Offset lineStart, Offset lineEnd) {
379-
double line1Slop = (lineEnd.dy - lineStart.dy) / (lineEnd.dx - lineStart.dx);
392+
double _getDistanceBetweenPointAndLine(
393+
Offset point, Offset lineStart, Offset lineEnd) {
394+
double line1Slop =
395+
(lineEnd.dy - lineStart.dy) / (lineEnd.dx - lineStart.dx);
380396

381397
if (line1Slop == 0) {
382398
return (point.dy - lineStart.dy).abs();
@@ -389,11 +405,13 @@ class _CustomImageCropState extends State<CustomImageCrop>
389405
return (Offset(crossPointX, crossPointY) - point).distance;
390406
}
391407

392-
bool _isContainPath(Rect initialImageRect, Rect pathRect, double currentScale) {
408+
bool _isContainPath(
409+
Rect initialImageRect, Rect pathRect, double currentScale) {
393410
final imageRect = _getImageRect(initialImageRect, currentScale);
394411
Offset topLeft, topRight, bottomLeft, bottomRight;
395412
final rad = atan(imageRect.height / imageRect.width);
396-
final len = sqrt(pow(imageRect.width / 2, 2) + pow(imageRect.height / 2, 2));
413+
final len =
414+
sqrt(pow(imageRect.width / 2, 2) + pow(imageRect.height / 2, 2));
397415
bool isRotated = data.angle != 0;
398416

399417
if (isRotated) {
@@ -404,9 +422,11 @@ class _CustomImageCropState extends State<CustomImageCrop>
404422
final cosCounterClockValue = len * cos(counterClockAngle);
405423
final sinCounterClockValue = len * sin(counterClockAngle);
406424
bottomRight = imageRect.center.translate(cosClockValue, sinClockValue);
407-
topRight = imageRect.center.translate(cosCounterClockValue, -sinCounterClockValue);
425+
topRight = imageRect.center
426+
.translate(cosCounterClockValue, -sinCounterClockValue);
408427
topLeft = imageRect.center.translate(-cosClockValue, -sinClockValue);
409-
bottomLeft = imageRect.center.translate(-cosCounterClockValue, sinCounterClockValue);
428+
bottomLeft = imageRect.center
429+
.translate(-cosCounterClockValue, sinCounterClockValue);
410430
} else {
411431
bottomRight = imageRect.bottomRight;
412432
topRight = imageRect.topRight;
@@ -417,10 +437,15 @@ class _CustomImageCropState extends State<CustomImageCrop>
417437
if (widget.shape == CustomCropShape.Circle) {
418438
final anchor = max(pathRect.width, pathRect.height) / 2;
419439
final pathCenter = pathRect.center;
420-
return _getDistanceBetweenPointAndLine(pathCenter, topLeft, topRight) >= anchor &&
421-
_getDistanceBetweenPointAndLine(pathCenter, topRight, bottomRight) >= anchor &&
422-
_getDistanceBetweenPointAndLine(pathCenter, bottomLeft, bottomRight) >= anchor &&
423-
_getDistanceBetweenPointAndLine(pathCenter, topLeft, bottomLeft) >= anchor;
440+
return _getDistanceBetweenPointAndLine(pathCenter, topLeft, topRight) >=
441+
anchor &&
442+
_getDistanceBetweenPointAndLine(pathCenter, topRight, bottomRight) >=
443+
anchor &&
444+
_getDistanceBetweenPointAndLine(
445+
pathCenter, bottomLeft, bottomRight) >=
446+
anchor &&
447+
_getDistanceBetweenPointAndLine(pathCenter, topLeft, bottomLeft) >=
448+
anchor;
424449
}
425450

426451
if (isRotated) {
@@ -442,20 +467,22 @@ class _CustomImageCropState extends State<CustomImageCrop>
442467
}
443468
}
444469

445-
double _calculateScaleAfterRotate(
446-
Rect pathRect, double startScale, Rect initialImageRect, double minEdgeHalf) {
470+
double _calculateScaleAfterRotate(Rect pathRect, double startScale,
471+
Rect initialImageRect, double minEdgeHalf) {
447472
final imageCenter = initialImageRect.center.translate(data.x, data.y);
448473
final topLeftDistance = (pathRect.topLeft - imageCenter).distance;
449474
final topRightDistance = (pathRect.topRight - imageCenter).distance;
450475
final bottomLeftDistance = (pathRect.bottomLeft - imageCenter).distance;
451476
final bottomRightDistance = (pathRect.bottomRight - imageCenter).distance;
452-
final maxDistance =
453-
max(max(max(topLeftDistance, topRightDistance), bottomLeftDistance), bottomRightDistance);
477+
final maxDistance = max(
478+
max(max(topLeftDistance, topRightDistance), bottomLeftDistance),
479+
bottomRightDistance);
454480
double endScale = maxDistance / minEdgeHalf;
455481

456482
if (startScale >= endScale) {
457483
return endScale;
458484
}
485+
459486
///use binary search to find best scale which just contain path.
460487
///Also, we can use imageCenter、imageLine(longest one) and path vertex to calculate.
461488
double step = 1 / minEdgeHalf;

0 commit comments

Comments
 (0)