@@ -141,7 +141,8 @@ class CustomImageCrop extends StatefulWidget {
141
141
Paint ? imagePaintDuringCrop,
142
142
this .forceInsideCropArea = false ,
143
143
Key ? key,
144
- }) : this .imagePaintDuringCrop = imagePaintDuringCrop ?? (Paint ()..filterQuality = FilterQuality .high),
144
+ }) : this .imagePaintDuringCrop = imagePaintDuringCrop ??
145
+ (Paint ()..filterQuality = FilterQuality .high),
145
146
assert (
146
147
! (shape == CustomCropShape .Ratio && ratio == null ),
147
148
"If shape is set to Ratio, ratio should not be null." ,
@@ -152,7 +153,8 @@ class CustomImageCrop extends StatefulWidget {
152
153
_CustomImageCropState createState () => _CustomImageCropState ();
153
154
}
154
155
155
- class _CustomImageCropState extends State <CustomImageCrop > with CustomImageCropListener {
156
+ class _CustomImageCropState extends State <CustomImageCrop >
157
+ with CustomImageCropListener {
156
158
CropImageData ? _dataTransitionStart;
157
159
late Path _path;
158
160
late Path _maskPath;
@@ -263,7 +265,8 @@ class _CustomImageCropState extends State<CustomImageCrop> with CustomImageCropL
263
265
left: data.x + _width / 2 ,
264
266
top: data.y + _height / 2 ,
265
267
child: Transform (
266
- transform: Matrix4 .diagonal3 (vector_math.Vector3 (scale, scale, scale))
268
+ transform: Matrix4 .diagonal3 (
269
+ vector_math.Vector3 (scale, scale, scale))
267
270
..rotateZ (data.angle)
268
271
..translate (- image.width / 2 , - image.height / 2 ),
269
272
child: Image (
@@ -293,7 +296,8 @@ class _CustomImageCropState extends State<CustomImageCrop> with CustomImageCropL
293
296
}
294
297
295
298
void onScaleUpdate (ScaleEvent event) {
296
- final scale = widget.canScale ? event.scale : (_dataTransitionStart? .scale ?? 1.0 );
299
+ final scale =
300
+ widget.canScale ? event.scale : (_dataTransitionStart? .scale ?? 1.0 );
297
301
298
302
final angle = widget.canRotate ? event.rotationAngle : 0.0 ;
299
303
@@ -319,7 +323,8 @@ class _CustomImageCropState extends State<CustomImageCrop> with CustomImageCropL
319
323
void onMoveUpdate (MoveEvent event) {
320
324
if (! widget.canMove) return ;
321
325
322
- widget.cropController.addTransition (CropImageData (x: event.delta.dx, y: event.delta.dy));
326
+ widget.cropController
327
+ .addTransition (CropImageData (x: event.delta.dx, y: event.delta.dy));
323
328
}
324
329
325
330
Rect _getInitialImageRect () {
@@ -335,7 +340,8 @@ class _CustomImageCropState extends State<CustomImageCrop> with CustomImageCropL
335
340
aspectRatio: (widget.ratio? .width ?? 1 ) / (widget.ratio? .height ?? 1 ),
336
341
);
337
342
final initialWidth = _imageAsUIImage! .width * cropFitParams.additionalScale;
338
- final initialHeight = _imageAsUIImage! .height * cropFitParams.additionalScale;
343
+ final initialHeight =
344
+ _imageAsUIImage! .height * cropFitParams.additionalScale;
339
345
return Rect .fromLTWH (
340
346
(_width - initialWidth) / 2 ,
341
347
(_height - initialHeight) / 2 ,
@@ -363,38 +369,53 @@ class _CustomImageCropState extends State<CustomImageCrop> with CustomImageCropL
363
369
364
370
if (transition.x != 0 || transition.y != 0 ) {
365
371
if (isRotated) {
366
- _addTransitionInternal (CropImageData (x: startX - data.x, y: startY - data.y));
372
+ _addTransitionInternal (
373
+ CropImageData (x: startX - data.x, y: startY - data.y));
367
374
} else {
368
375
final imageRect = _getImageRect (initialImageRect, data.scale);
369
376
double deltaX = min (pathRect.left - imageRect.left, 0 );
370
- deltaX = pathRect.right > imageRect.right ? pathRect.right - imageRect.right : deltaX;
377
+ deltaX = pathRect.right > imageRect.right
378
+ ? pathRect.right - imageRect.right
379
+ : deltaX;
371
380
double deltaY = min (pathRect.top - imageRect.top, 0 );
372
- deltaY = pathRect.bottom > imageRect.bottom ? pathRect.bottom - imageRect.bottom : deltaY;
381
+ deltaY = pathRect.bottom > imageRect.bottom
382
+ ? pathRect.bottom - imageRect.bottom
383
+ : deltaY;
373
384
_addTransitionInternal (CropImageData (x: deltaX, y: deltaY));
374
385
}
375
386
return ;
376
387
}
377
- double minEdgeHalf = min (initialImageRect.width, initialImageRect.height) / 2 ;
378
- double adaptScale = _calculateScaleAfterRotate (pathRect, data.scale, initialImageRect, minEdgeHalf);
388
+ double minEdgeHalf =
389
+ min (initialImageRect.width, initialImageRect.height) / 2 ;
390
+ double adaptScale = _calculateScaleAfterRotate (
391
+ pathRect, data.scale, initialImageRect, minEdgeHalf);
379
392
_addTransitionInternal (CropImageData (scale: adaptScale / data.scale));
380
393
}
381
394
382
395
Rect _getImageRect (Rect initialImageRect, double currentScale) {
383
396
final diffScale = (1 - currentScale) / 2 ;
384
- final left = initialImageRect.left + diffScale * initialImageRect.width + data.x;
385
- final top = initialImageRect.top + diffScale * initialImageRect.height + data.y;
386
- Rect imageRect = Rect .fromLTWH (left, top, currentScale * initialImageRect.width, currentScale * initialImageRect.height);
397
+ final left =
398
+ initialImageRect.left + diffScale * initialImageRect.width + data.x;
399
+ final top =
400
+ initialImageRect.top + diffScale * initialImageRect.height + data.y;
401
+ Rect imageRect = Rect .fromLTWH (
402
+ left,
403
+ top,
404
+ currentScale * initialImageRect.width,
405
+ currentScale * initialImageRect.height);
387
406
return imageRect;
388
407
}
389
408
390
- double _getDistanceBetweenPointAndLine (Offset point, Offset lineStart, Offset lineEnd) {
409
+ double _getDistanceBetweenPointAndLine (
410
+ Offset point, Offset lineStart, Offset lineEnd) {
391
411
if (lineEnd.dy == lineStart.dy) {
392
412
return (point.dy - lineStart.dy).abs ();
393
413
}
394
414
if (lineEnd.dx == lineStart.dx) {
395
415
return (point.dx - lineStart.dx).abs ();
396
416
}
397
- double line1Slop = (lineEnd.dy - lineStart.dy) / (lineEnd.dx - lineStart.dx);
417
+ double line1Slop =
418
+ (lineEnd.dy - lineStart.dy) / (lineEnd.dx - lineStart.dx);
398
419
double line1Delta = lineEnd.dy - lineEnd.dx * line1Slop;
399
420
double line2Slop = - 1 / line1Slop;
400
421
double line2Delta = point.dy - point.dx * line2Slop;
@@ -403,11 +424,13 @@ class _CustomImageCropState extends State<CustomImageCrop> with CustomImageCropL
403
424
return (Offset (crossPointX, crossPointY) - point).distance;
404
425
}
405
426
406
- bool _isContainPath (Rect initialImageRect, Rect pathRect, double currentScale) {
427
+ bool _isContainPath (
428
+ Rect initialImageRect, Rect pathRect, double currentScale) {
407
429
final imageRect = _getImageRect (initialImageRect, currentScale);
408
430
Offset topLeft, topRight, bottomLeft, bottomRight;
409
431
final rad = atan (imageRect.height / imageRect.width);
410
- final len = sqrt (pow (imageRect.width / 2 , 2 ) + pow (imageRect.height / 2 , 2 ));
432
+ final len =
433
+ sqrt (pow (imageRect.width / 2 , 2 ) + pow (imageRect.height / 2 , 2 ));
411
434
bool isRotated = data.angle != 0 ;
412
435
413
436
if (isRotated) {
@@ -418,9 +441,11 @@ class _CustomImageCropState extends State<CustomImageCrop> with CustomImageCropL
418
441
final cosCounterClockValue = len * cos (counterClockAngle);
419
442
final sinCounterClockValue = len * sin (counterClockAngle);
420
443
bottomRight = imageRect.center.translate (cosClockValue, sinClockValue);
421
- topRight = imageRect.center.translate (cosCounterClockValue, - sinCounterClockValue);
444
+ topRight = imageRect.center
445
+ .translate (cosCounterClockValue, - sinCounterClockValue);
422
446
topLeft = imageRect.center.translate (- cosClockValue, - sinClockValue);
423
- bottomLeft = imageRect.center.translate (- cosCounterClockValue, sinCounterClockValue);
447
+ bottomLeft = imageRect.center
448
+ .translate (- cosCounterClockValue, sinCounterClockValue);
424
449
} else {
425
450
bottomRight = imageRect.bottomRight;
426
451
topRight = imageRect.topRight;
@@ -431,10 +456,15 @@ class _CustomImageCropState extends State<CustomImageCrop> with CustomImageCropL
431
456
if (widget.shape == CustomCropShape .Circle ) {
432
457
final anchor = max (pathRect.width, pathRect.height) / 2 ;
433
458
final pathCenter = pathRect.center;
434
- return _getDistanceBetweenPointAndLine (pathCenter, topLeft, topRight) >= anchor &&
435
- _getDistanceBetweenPointAndLine (pathCenter, topRight, bottomRight) >= anchor &&
436
- _getDistanceBetweenPointAndLine (pathCenter, bottomLeft, bottomRight) >= anchor &&
437
- _getDistanceBetweenPointAndLine (pathCenter, topLeft, bottomLeft) >= anchor;
459
+ return _getDistanceBetweenPointAndLine (pathCenter, topLeft, topRight) >=
460
+ anchor &&
461
+ _getDistanceBetweenPointAndLine (pathCenter, topRight, bottomRight) >=
462
+ anchor &&
463
+ _getDistanceBetweenPointAndLine (
464
+ pathCenter, bottomLeft, bottomRight) >=
465
+ anchor &&
466
+ _getDistanceBetweenPointAndLine (pathCenter, topLeft, bottomLeft) >=
467
+ anchor;
438
468
}
439
469
440
470
if (isRotated) {
@@ -444,19 +474,28 @@ class _CustomImageCropState extends State<CustomImageCrop> with CustomImageCropL
444
474
..lineTo (bottomRight.dx, bottomRight.dy)
445
475
..lineTo (bottomLeft.dx, bottomLeft.dy)
446
476
..close ();
447
- return imagePath.contains (pathRect.topLeft) && imagePath.contains (pathRect.topRight) && imagePath.contains (pathRect.bottomLeft) && imagePath.contains (pathRect.bottomRight);
477
+ return imagePath.contains (pathRect.topLeft) &&
478
+ imagePath.contains (pathRect.topRight) &&
479
+ imagePath.contains (pathRect.bottomLeft) &&
480
+ imagePath.contains (pathRect.bottomRight);
448
481
} else {
449
- return imageRect.contains (pathRect.topLeft) && imageRect.contains (pathRect.topRight) && imageRect.contains (pathRect.bottomLeft) && imageRect.contains (pathRect.bottomRight);
482
+ return imageRect.contains (pathRect.topLeft) &&
483
+ imageRect.contains (pathRect.topRight) &&
484
+ imageRect.contains (pathRect.bottomLeft) &&
485
+ imageRect.contains (pathRect.bottomRight);
450
486
}
451
487
}
452
488
453
- double _calculateScaleAfterRotate (Rect pathRect, double startScale, Rect initialImageRect, double minEdgeHalf) {
489
+ double _calculateScaleAfterRotate (Rect pathRect, double startScale,
490
+ Rect initialImageRect, double minEdgeHalf) {
454
491
final imageCenter = initialImageRect.center.translate (data.x, data.y);
455
492
final topLeftDistance = (pathRect.topLeft - imageCenter).distance;
456
493
final topRightDistance = (pathRect.topRight - imageCenter).distance;
457
494
final bottomLeftDistance = (pathRect.bottomLeft - imageCenter).distance;
458
495
final bottomRightDistance = (pathRect.bottomRight - imageCenter).distance;
459
- final maxDistance = max (max (max (topLeftDistance, topRightDistance), bottomLeftDistance), bottomRightDistance);
496
+ final maxDistance = max (
497
+ max (max (topLeftDistance, topRightDistance), bottomLeftDistance),
498
+ bottomRightDistance);
460
499
double endScale = maxDistance / minEdgeHalf;
461
500
462
501
if (startScale >= endScale) {
0 commit comments