Skip to content

Commit 0b90c72

Browse files
author
Skandar Munir Ahmed
committed
fixes #33 borderRadius
1 parent d9cc318 commit 0b90c72

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ If ` shape`` is set to `CustomCropShape.Ratio`, this property is required.
6868
For example, to create a square crop area, use `[`Ratio(width: 1, height: 1)`.
6969
To create a rectangular crop area with a 16:9 aspect ratio, use `[`Ratio(width: 16, height: 9)`.
7070

71+
### borderRadius
72+
The radius for rounded corners of the cropping area.
73+
74+
7175
# Controller Methods
7276

7377
## addTransition

example/lib/main.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ class _MyHomePageState extends State<MyHomePage> {
9999
canRotate: true,
100100
canMove: false,
101101
canScale: false,
102+
// user borderRadius to smooth out corners if needed
103+
// borderRadius: 16,
102104
customProgressIndicator: const CupertinoActivityIndicator(),
103105
// use custom paint if needed
104106
// pathPaint: Paint()

lib/src/widgets/custom_image_crop_widget.dart

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ class CustomImageCrop extends StatefulWidget {
6767
/// - Stroke Width: 4.0
6868
final Paint? pathPaint;
6969

70+
/// The radius for rounded corners of the cropping area (only applicable to rounded rectangle shapes).
71+
final double borderRadius;
72+
7073
/// Whether to allow the image to be rotated.
7174
final bool canRotate;
7275

@@ -119,6 +122,7 @@ class CustomImageCrop extends StatefulWidget {
119122
this.canMove = true,
120123
this.customProgressIndicator,
121124
this.ratio,
125+
this.borderRadius = 0,
122126
Paint? imagePaintDuringCrop,
123127
Key? key,
124128
}) : this.imagePaintDuringCrop = imagePaintDuringCrop ??
@@ -202,7 +206,8 @@ class _CustomImageCropState extends State<CustomImageCrop>
202206
screenWidth: _width,
203207
);
204208
final scale = data.scale * cropParams.additionalScale;
205-
_path = _getPath(cropParams.cropSizeToPaint, _width, _height);
209+
_path = _getPath(
210+
cropParams.cropSizeToPaint, _width, _height, widget.borderRadius);
206211
return XGestureDetector(
207212
onMoveStart: onMoveStart,
208213
onMoveUpdate: onMoveUpdate,
@@ -279,7 +284,8 @@ class _CustomImageCropState extends State<CustomImageCrop>
279284
addTransition(CropImageData(x: event.delta.dx, y: event.delta.dy));
280285
}
281286

282-
Path _getPath(double cropWidth, double width, double height) {
287+
Path _getPath(
288+
double cropWidth, double width, double height, double borderRadius) {
283289
switch (widget.shape) {
284290
case CustomCropShape.Circle:
285291
return Path()
@@ -291,20 +297,26 @@ class _CustomImageCropState extends State<CustomImageCrop>
291297
);
292298
case CustomCropShape.Ratio:
293299
return Path()
294-
..addRect(
295-
Rect.fromCenter(
296-
center: Offset(width / 2, height / 2),
297-
width: cropWidth,
298-
height: cropWidth * widget.ratio!.height / widget.ratio!.width,
300+
..addRRect(
301+
RRect.fromRectAndRadius(
302+
Rect.fromCenter(
303+
center: Offset(width / 2, height / 2),
304+
width: cropWidth,
305+
height: cropWidth * widget.ratio!.height / widget.ratio!.width,
306+
),
307+
Radius.circular(borderRadius),
299308
),
300309
);
301310
default:
302311
return Path()
303-
..addRect(
304-
Rect.fromCenter(
305-
center: Offset(width / 2, height / 2),
306-
width: cropWidth,
307-
height: cropWidth,
312+
..addRRect(
313+
RRect.fromRectAndRadius(
314+
Rect.fromCenter(
315+
center: Offset(width / 2, height / 2),
316+
width: cropWidth,
317+
height: cropWidth,
318+
),
319+
Radius.circular(borderRadius),
308320
),
309321
);
310322
}
@@ -328,8 +340,8 @@ class _CustomImageCropState extends State<CustomImageCrop>
328340
screenWidth: _width,
329341
dataScale: data.scale,
330342
);
331-
final clipPath = Path.from(_getPath(
332-
onCropParams.cropSize, onCropParams.cropSize, onCropParams.cropSize));
343+
final clipPath = Path.from(_getPath(onCropParams.cropSize,
344+
onCropParams.cropSize, onCropParams.cropSize, widget.borderRadius));
333345
final matrix4Image = Matrix4.diagonal3(vector_math.Vector3.all(1))
334346
..translate(
335347
onCropParams.translateScale * data.x + onCropParams.cropSize / 2,

0 commit comments

Comments
 (0)