Skip to content

Commit

Permalink
Merge pull request #1 from muraikenta/enable-circle-crop
Browse files Browse the repository at this point in the history
サークルのマスクにできるように
  • Loading branch information
muraikenta authored Jul 14, 2021
2 parents 04635f1 + d250f0c commit dfc6833
Showing 1 changed file with 58 additions and 34 deletions.
92 changes: 58 additions & 34 deletions lib/src/crop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Crop extends StatefulWidget {
final double maximumScale;
final bool alwaysShowGrid;
final ImageErrorListener? onImageError;
final BoxShape shape;

const Crop({
Key? key,
Expand All @@ -27,6 +28,7 @@ class Crop extends StatefulWidget {
this.maximumScale = 2.0,
this.alwaysShowGrid = false,
this.onImageError,
this.shape = BoxShape.rectangle,
}) : super(key: key);

Crop.file(
Expand All @@ -37,6 +39,7 @@ class Crop extends StatefulWidget {
this.maximumScale = 2.0,
this.alwaysShowGrid = false,
this.onImageError,
this.shape = BoxShape.rectangle,
}) : image = FileImage(file, scale: scale),
super(key: key);

Expand All @@ -49,6 +52,7 @@ class Crop extends StatefulWidget {
this.maximumScale = 2.0,
this.alwaysShowGrid = false,
this.onImageError,
this.shape = BoxShape.rectangle,
}) : image = AssetImage(assetName, bundle: bundle, package: package),
super(key: key);

Expand Down Expand Up @@ -192,6 +196,7 @@ class CropState extends State<Crop> with TickerProviderStateMixin, Drag {
area: _area,
scale: _scale,
active: _activeController.value,
shape: widget.shape,
),
),
),
Expand Down Expand Up @@ -624,6 +629,7 @@ class _CropPainter extends CustomPainter {
final Rect area;
final double scale;
final double active;
final BoxShape shape;

_CropPainter({
required this.image,
Expand All @@ -632,6 +638,7 @@ class _CropPainter extends CustomPainter {
required this.area,
required this.scale,
required this.active,
required this.shape,
});

@override
Expand Down Expand Up @@ -691,16 +698,27 @@ class _CropPainter extends CustomPainter {
rect.width * area.width,
rect.height * area.height,
);
canvas.drawRect(Rect.fromLTRB(0.0, 0.0, rect.width, boundaries.top), paint);
canvas.drawRect(
Rect.fromLTRB(0.0, boundaries.bottom, rect.width, rect.height), paint);
canvas.drawRect(
Rect.fromLTRB(0.0, boundaries.top, boundaries.left, boundaries.bottom),
paint);
canvas.drawRect(
Rect.fromLTRB(
boundaries.right, boundaries.top, rect.width, boundaries.bottom),
paint);
if (shape == BoxShape.circle) {
canvas.saveLayer(null, Paint()..blendMode = BlendMode.multiply);
var pa = Paint()..blendMode = BlendMode.clear;
canvas.drawRect(Rect.fromLTRB(0.0, 0.0, rect.width, rect.bottom), paint);
canvas.drawCircle(boundaries.center, boundaries.height / 2, pa);
canvas.restore();
} else {
canvas.drawRect(
Rect.fromLTRB(0.0, 0.0, rect.width, boundaries.top), paint);
canvas.drawRect(
Rect.fromLTRB(0.0, boundaries.bottom, rect.width, rect.height),
paint);
canvas.drawRect(
Rect.fromLTRB(
0.0, boundaries.top, boundaries.left, boundaries.bottom),
paint);
canvas.drawRect(
Rect.fromLTRB(
boundaries.right, boundaries.top, rect.width, boundaries.bottom),
paint);
}

if (boundaries.isEmpty == false) {
_drawGrid(canvas, boundaries);
Expand Down Expand Up @@ -765,31 +783,37 @@ class _CropPainter extends CustomPainter {
..style = PaintingStyle.stroke
..strokeWidth = 1.0;

final path = Path()
..moveTo(boundaries.left, boundaries.top)
..lineTo(boundaries.right, boundaries.top)
..lineTo(boundaries.right, boundaries.bottom)
..lineTo(boundaries.left, boundaries.bottom)
..lineTo(boundaries.left, boundaries.top);

for (var column = 1; column < _kCropGridColumnCount; column++) {
path
..moveTo(
boundaries.left + column * boundaries.width / _kCropGridColumnCount,
boundaries.top)
..lineTo(
boundaries.left + column * boundaries.width / _kCropGridColumnCount,
boundaries.bottom);
}
if (shape == BoxShape.circle) {
final path = Path()..addOval(boundaries);
canvas.drawPath(path, paint);
} else {
final path = Path()
..moveTo(boundaries.left, boundaries.top)
..lineTo(boundaries.right, boundaries.top)
..lineTo(boundaries.right, boundaries.bottom)
..lineTo(boundaries.left, boundaries.bottom)
..lineTo(boundaries.left, boundaries.top);

for (var column = 1; column < _kCropGridColumnCount; column++) {
path
..moveTo(
boundaries.left +
column * boundaries.width / _kCropGridColumnCount,
boundaries.top)
..lineTo(
boundaries.left +
column * boundaries.width / _kCropGridColumnCount,
boundaries.bottom);
}

for (var row = 1; row < _kCropGridRowCount; row++) {
path
..moveTo(boundaries.left,
boundaries.top + row * boundaries.height / _kCropGridRowCount)
..lineTo(boundaries.right,
boundaries.top + row * boundaries.height / _kCropGridRowCount);
for (var row = 1; row < _kCropGridRowCount; row++) {
path
..moveTo(boundaries.left,
boundaries.top + row * boundaries.height / _kCropGridRowCount)
..lineTo(boundaries.right,
boundaries.top + row * boundaries.height / _kCropGridRowCount);
}
canvas.drawPath(path, paint);
}

canvas.drawPath(path, paint);
}
}

0 comments on commit dfc6833

Please sign in to comment.