Skip to content

Commit

Permalink
fix: several bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
chooyan-eng committed Dec 12, 2024
1 parent 35a425b commit 9d04f82
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
22 changes: 18 additions & 4 deletions lib/src/logic/cropper/image_image_cropper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ final RectCropper<Image> defaultRectCropper = (
required Size size,
required ImageFormat? outputFormat,
}) {
return _findCropFunc(outputFormat)(
return _findEncodeFunc(outputFormat)(
copyCrop(
original,
x: topLeft.dx.toInt(),
Expand All @@ -46,17 +46,21 @@ final CircleCropper<Image> defaultCircleCropper = (
required double radius,
required ImageFormat? outputFormat,
}) {
return _findCropFunc(outputFormat)(
// convert to rgba if necessary
final target =
original.numChannels == 4 ? original : original.convert(numChannels: 4);

return _findCircleEncodeFunc(outputFormat)(
copyCropCircle(
original,
target,
centerX: center.dx.toInt(),
centerY: center.dy.toInt(),
radius: radius.toInt(),
),
);
};

Uint8List Function(Image) _findCropFunc(ImageFormat? outputFormat) {
Uint8List Function(Image) _findEncodeFunc(ImageFormat? outputFormat) {
return switch (outputFormat) {
ImageFormat.bmp => encodeBmp,
ImageFormat.ico => encodeIco,
Expand All @@ -65,3 +69,13 @@ Uint8List Function(Image) _findCropFunc(ImageFormat? outputFormat) {
_ => encodePng,
};
}

Uint8List Function(Image) _findCircleEncodeFunc(ImageFormat? outputFormat) {
return switch (outputFormat) {
ImageFormat.bmp => encodeBmp,
ImageFormat.ico => encodeIco,
ImageFormat.jpeg => encodePng, // jpeg does not support circle crop
ImageFormat.png => encodePng,
_ => encodePng,
};
}
6 changes: 5 additions & 1 deletion lib/src/logic/cropper/legacy_image_image_cropper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ final CircleCropper<Image> legacyCircleCropper = (
required double radius,
required ImageFormat? outputFormat,
}) {
// convert to rgba if necessary
final target =
original.numChannels == 4 ? original : original.convert(numChannels: 4);

return encodePng(
copyCropCircle(
original,
target,
centerX: center.dx.toInt(),
centerY: center.dy.toInt(),
radius: radius.toInt(),
Expand Down
31 changes: 14 additions & 17 deletions lib/src/widget/crop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,13 @@ class _CropEditorState extends State<_CropEditor> {
return;
}
if (parsed != null) {
setState(() {
_viewState = (_viewState as PreparingCropEditorViewState).prepared(
Size(parsed.width, parsed.height),
);
});
if (_viewState is PreparingCropEditorViewState) {
setState(() {
_viewState = (_viewState as PreparingCropEditorViewState).prepared(
Size(parsed.width, parsed.height),
);
});
}
_resetCropRect();
widget.onStatusChanged?.call(CropStatus.ready);
}
Expand All @@ -374,13 +376,6 @@ class _CropEditorState extends State<_CropEditor> {

widget.onStatusChanged?.call(CropStatus.loading);

/// reset view state back to preparing state
_viewState = PreparingCropEditorViewState(
viewportSize: MediaQuery.of(context).size,
withCircleUi: widget.withCircleUi,
aspectRatio: widget.aspectRatio,
);

_parseImageWith(
parser: widget.imageParser,
formatDetector: widget.formatDetector,
Expand All @@ -390,11 +385,13 @@ class _CropEditorState extends State<_CropEditor> {
return;
}
if (parsed != null) {
setState(() {
_viewState = (_viewState as PreparingCropEditorViewState).prepared(
Size(parsed.width, parsed.height),
);
});
if (_viewState is PreparingCropEditorViewState) {
setState(() {
_viewState = (_viewState as PreparingCropEditorViewState).prepared(
Size(parsed.width, parsed.height),
);
});
}
_resetCropRect();
widget.onStatusChanged?.call(CropStatus.ready);
}
Expand Down

0 comments on commit 9d04f82

Please sign in to comment.