@@ -133,8 +133,8 @@ class CustomImageCrop extends StatefulWidget {
133
133
Paint ? imagePaintDuringCrop,
134
134
this .forceInsideCropArea = false ,
135
135
Key ? key,
136
- }) : this .imagePaintDuringCrop =
137
- imagePaintDuringCrop ?? (Paint ()..filterQuality = FilterQuality .high),
136
+ }) : this .imagePaintDuringCrop = imagePaintDuringCrop ??
137
+ (Paint ()..filterQuality = FilterQuality .high),
138
138
assert (
139
139
! (shape == CustomCropShape .Ratio && ratio == null ),
140
140
"If shape is set to Ratio, ratio should not be null." ,
@@ -301,7 +301,8 @@ class _CustomImageCropState extends State<CustomImageCrop>
301
301
void onMoveUpdate (MoveEvent event) {
302
302
if (! widget.canMove) return ;
303
303
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));
305
306
}
306
307
307
308
Rect _getInitialImageRect () {
@@ -317,7 +318,8 @@ class _CustomImageCropState extends State<CustomImageCrop>
317
318
aspectRatio: (widget.ratio? .width ?? 1 ) / (widget.ratio? .height ?? 1 ),
318
319
);
319
320
final initialWidth = _imageAsUIImage! .width * cropFitParams.additionalScale;
320
- final initialHeight = _imageAsUIImage! .height * cropFitParams.additionalScale;
321
+ final initialHeight =
322
+ _imageAsUIImage! .height * cropFitParams.additionalScale;
321
323
return Rect .fromLTWH (
322
324
(_width - initialWidth) / 2 ,
323
325
(_height - initialHeight) / 2 ,
@@ -346,13 +348,18 @@ class _CustomImageCropState extends State<CustomImageCrop>
346
348
347
349
if (transition.x != 0 || transition.y != 0 ) {
348
350
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));
350
353
} else {
351
354
final imageRect = _getImageRect (initialImageRect, data.scale);
352
355
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;
354
359
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;
356
363
_addTransitionInternal (CropImageData (x: deltaX, y: deltaY));
357
364
}
358
365
return ;
@@ -361,22 +368,31 @@ class _CustomImageCropState extends State<CustomImageCrop>
361
368
_addTransitionInternal (CropImageData (scale: startScale / data.scale));
362
369
return ;
363
370
}
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);
366
375
_addTransitionInternal (CropImageData (scale: adaptScale / data.scale));
367
376
}
368
377
369
378
Rect _getImageRect (Rect initialImageRect, double currentScale) {
370
379
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;
373
384
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);
375
389
return imageRect;
376
390
}
377
391
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);
380
396
381
397
if (line1Slop == 0 ) {
382
398
return (point.dy - lineStart.dy).abs ();
@@ -389,11 +405,13 @@ class _CustomImageCropState extends State<CustomImageCrop>
389
405
return (Offset (crossPointX, crossPointY) - point).distance;
390
406
}
391
407
392
- bool _isContainPath (Rect initialImageRect, Rect pathRect, double currentScale) {
408
+ bool _isContainPath (
409
+ Rect initialImageRect, Rect pathRect, double currentScale) {
393
410
final imageRect = _getImageRect (initialImageRect, currentScale);
394
411
Offset topLeft, topRight, bottomLeft, bottomRight;
395
412
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 ));
397
415
bool isRotated = data.angle != 0 ;
398
416
399
417
if (isRotated) {
@@ -404,9 +422,11 @@ class _CustomImageCropState extends State<CustomImageCrop>
404
422
final cosCounterClockValue = len * cos (counterClockAngle);
405
423
final sinCounterClockValue = len * sin (counterClockAngle);
406
424
bottomRight = imageRect.center.translate (cosClockValue, sinClockValue);
407
- topRight = imageRect.center.translate (cosCounterClockValue, - sinCounterClockValue);
425
+ topRight = imageRect.center
426
+ .translate (cosCounterClockValue, - sinCounterClockValue);
408
427
topLeft = imageRect.center.translate (- cosClockValue, - sinClockValue);
409
- bottomLeft = imageRect.center.translate (- cosCounterClockValue, sinCounterClockValue);
428
+ bottomLeft = imageRect.center
429
+ .translate (- cosCounterClockValue, sinCounterClockValue);
410
430
} else {
411
431
bottomRight = imageRect.bottomRight;
412
432
topRight = imageRect.topRight;
@@ -417,10 +437,15 @@ class _CustomImageCropState extends State<CustomImageCrop>
417
437
if (widget.shape == CustomCropShape .Circle ) {
418
438
final anchor = max (pathRect.width, pathRect.height) / 2 ;
419
439
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;
424
449
}
425
450
426
451
if (isRotated) {
@@ -442,20 +467,22 @@ class _CustomImageCropState extends State<CustomImageCrop>
442
467
}
443
468
}
444
469
445
- double _calculateScaleAfterRotate (
446
- Rect pathRect, double startScale, Rect initialImageRect, double minEdgeHalf) {
470
+ double _calculateScaleAfterRotate (Rect pathRect, double startScale,
471
+ Rect initialImageRect, double minEdgeHalf) {
447
472
final imageCenter = initialImageRect.center.translate (data.x, data.y);
448
473
final topLeftDistance = (pathRect.topLeft - imageCenter).distance;
449
474
final topRightDistance = (pathRect.topRight - imageCenter).distance;
450
475
final bottomLeftDistance = (pathRect.bottomLeft - imageCenter).distance;
451
476
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);
454
480
double endScale = maxDistance / minEdgeHalf;
455
481
456
482
if (startScale >= endScale) {
457
483
return endScale;
458
484
}
485
+
459
486
///use binary search to find best scale which just contain path.
460
487
///Also, we can use imageCenter、imageLine(longest one) and path vertex to calculate.
461
488
double step = 1 / minEdgeHalf;
0 commit comments