Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

サークルのマスクにできるように #1

Merged
merged 1 commit into from
Jul 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}