Skip to content

Commit

Permalink
Merge pull request #34 from sikandernoori/main
Browse files Browse the repository at this point in the history
#33 borderRadius
  • Loading branch information
ikbendewilliam committed Aug 10, 2023
2 parents d9cc318 + 0b90c72 commit d6b90fa
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ If ` shape`` is set to `CustomCropShape.Ratio`, this property is required.
For example, to create a square crop area, use `[`Ratio(width: 1, height: 1)`.
To create a rectangular crop area with a 16:9 aspect ratio, use `[`Ratio(width: 16, height: 9)`.

### borderRadius
The radius for rounded corners of the cropping area.


# Controller Methods

## addTransition
Expand Down
2 changes: 2 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ class _MyHomePageState extends State<MyHomePage> {
canRotate: true,
canMove: false,
canScale: false,
// user borderRadius to smooth out corners if needed
// borderRadius: 16,
customProgressIndicator: const CupertinoActivityIndicator(),
// use custom paint if needed
// pathPaint: Paint()
Expand Down
40 changes: 26 additions & 14 deletions lib/src/widgets/custom_image_crop_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class CustomImageCrop extends StatefulWidget {
/// - Stroke Width: 4.0
final Paint? pathPaint;

/// The radius for rounded corners of the cropping area (only applicable to rounded rectangle shapes).
final double borderRadius;

/// Whether to allow the image to be rotated.
final bool canRotate;

Expand Down Expand Up @@ -119,6 +122,7 @@ class CustomImageCrop extends StatefulWidget {
this.canMove = true,
this.customProgressIndicator,
this.ratio,
this.borderRadius = 0,
Paint? imagePaintDuringCrop,
Key? key,
}) : this.imagePaintDuringCrop = imagePaintDuringCrop ??
Expand Down Expand Up @@ -202,7 +206,8 @@ class _CustomImageCropState extends State<CustomImageCrop>
screenWidth: _width,
);
final scale = data.scale * cropParams.additionalScale;
_path = _getPath(cropParams.cropSizeToPaint, _width, _height);
_path = _getPath(
cropParams.cropSizeToPaint, _width, _height, widget.borderRadius);
return XGestureDetector(
onMoveStart: onMoveStart,
onMoveUpdate: onMoveUpdate,
Expand Down Expand Up @@ -279,7 +284,8 @@ class _CustomImageCropState extends State<CustomImageCrop>
addTransition(CropImageData(x: event.delta.dx, y: event.delta.dy));
}

Path _getPath(double cropWidth, double width, double height) {
Path _getPath(
double cropWidth, double width, double height, double borderRadius) {
switch (widget.shape) {
case CustomCropShape.Circle:
return Path()
Expand All @@ -291,20 +297,26 @@ class _CustomImageCropState extends State<CustomImageCrop>
);
case CustomCropShape.Ratio:
return Path()
..addRect(
Rect.fromCenter(
center: Offset(width / 2, height / 2),
width: cropWidth,
height: cropWidth * widget.ratio!.height / widget.ratio!.width,
..addRRect(
RRect.fromRectAndRadius(
Rect.fromCenter(
center: Offset(width / 2, height / 2),
width: cropWidth,
height: cropWidth * widget.ratio!.height / widget.ratio!.width,
),
Radius.circular(borderRadius),
),
);
default:
return Path()
..addRect(
Rect.fromCenter(
center: Offset(width / 2, height / 2),
width: cropWidth,
height: cropWidth,
..addRRect(
RRect.fromRectAndRadius(
Rect.fromCenter(
center: Offset(width / 2, height / 2),
width: cropWidth,
height: cropWidth,
),
Radius.circular(borderRadius),
),
);
}
Expand All @@ -328,8 +340,8 @@ class _CustomImageCropState extends State<CustomImageCrop>
screenWidth: _width,
dataScale: data.scale,
);
final clipPath = Path.from(_getPath(
onCropParams.cropSize, onCropParams.cropSize, onCropParams.cropSize));
final clipPath = Path.from(_getPath(onCropParams.cropSize,
onCropParams.cropSize, onCropParams.cropSize, widget.borderRadius));
final matrix4Image = Matrix4.diagonal3(vector_math.Vector3.all(1))
..translate(
onCropParams.translateScale * data.x + onCropParams.cropSize / 2,
Expand Down

0 comments on commit d6b90fa

Please sign in to comment.