openCropEditor method
void
openCropEditor()
Opens the crop editor.
This method opens the crop editor, allowing the user to crop and rotate the image.
Implementation
void openCropEditor() async {
if (_activeCrop) return;
EditorImage img = EditorImage(
assetPath: _image.assetPath,
byteArray: _image.byteArray,
file: _image.file,
networkUrl: _image.networkUrl,
);
Uint8List? bytesWithLayers;
if (_layers.isNotEmpty) {
_activeCrop = true;
LoadingDialog loading = LoadingDialog()
..show(
context,
theme: _theme,
imageEditorTheme: widget.configs.imageEditorTheme,
designMode: widget.configs.designMode,
i18n: widget.configs.i18n,
message: widget.configs.i18n.cropRotateEditor.prepareImageDialogMsg,
);
bytesWithLayers = await _screenshotCtrl.capture(
pixelRatio: _pixelRatio,
);
if (mounted) await loading.hide(context);
}
_activeCrop = false;
if (!mounted) return;
_openEditor = true;
_openPage<CropRotateEditorRes?>(
CropRotateEditor.autoSource(
file: img.file,
byteArray: img.byteArray,
assetPath: img.assetPath,
networkUrl: img.networkUrl,
bytesWithLayers: bytesWithLayers,
i18n: widget.configs.i18n,
icons: widget.configs.icons,
theme: _theme,
imageSize: Size(_imageWidth, _imageHeight),
heroTag: widget.configs.heroTag,
designMode: widget.configs.designMode,
imageEditorTheme: widget.configs.imageEditorTheme,
customWidgets: widget.configs.customWidgets,
configs: widget.configs.cropRotateEditorConfigs,
),
).then((response) async {
_openEditor = false;
if (response != null) {
CropRotateEditorResponse res = response.result;
if (res.bytes != null) {
var decodedImage = response.image;
if (!mounted) return;
var w = decodedImage.width;
var h = decodedImage.height;
var widthRatio = w.toDouble() / _screen.width;
var heightRatio = h.toDouble() / _screenInnerHeight;
var newPixelRatio = max(heightRatio, widthRatio);
var newImgW = w / newPixelRatio;
var newImgH = h / newPixelRatio;
var scale = (_imageWidth * _pixelRatio) / w;
var oldFullW = _imageWidth * _pixelRatio;
var oldFullH = _imageHeight * _pixelRatio;
var rotationScale = _imageWidth / newImgH;
double fitFactor = 1;
bool oldFitWidth = _imageWidth >= _screen.width - 0.1 &&
_imageWidth <= _screen.width + 0.1;
bool newFitWidth =
newImgW >= _screen.width - 0.1 && newImgW <= _screen.width + 0.1;
var scaleX = newFitWidth ? oldFullW / w : oldFullH / h;
if (oldFitWidth != newFitWidth) {
if (newFitWidth) {
fitFactor = _imageWidth / newImgW;
} else {
fitFactor = _imageHeight / newImgH;
}
}
List<Layer> updatedLayers = [];
for (var el in _layers) {
var layer = _copyLayer(el);
var beforeIsFlipX = layer.flipX;
switch (res.rotationAngle) {
case 0:
case 180:
layer.offset = Offset(
layer.offset.dx / fitFactor,
layer.offset.dy / fitFactor,
);
break;
case 180:
layer.offset = Offset(
layer.offset.dx / fitFactor,
layer.offset.dy / fitFactor,
);
break;
default:
}
bool zoomed = _zoomedLayer(
layer: layer,
scale: scale,
scaleX: scaleX,
oldFullH: oldFullH,
oldFullW: oldFullW,
cropRect: res.cropRect,
isHalfPi: res.isHalfPi,
);
_flipLayer(
layer: layer,
flipX: res.flipX,
flipY: res.flipY,
isHalfPi: res.isHalfPi,
);
_rotateLayer(
layer: layer,
beforeIsFlipX: beforeIsFlipX,
newImgW: newImgW,
newImgH: newImgH,
rotationAngle: res.rotationAngle,
rotationRadian: res.rotationRadian,
rotationScale: zoomed ? 1 : rotationScale,
);
updatedLayers.add(layer);
}
_addCroppedImg(updatedLayers, EditorImage(byteArray: res.bytes));
_pixelRatio = max(heightRatio, widthRatio);
_imageWidth = w / _pixelRatio;
_imageHeight = h / _pixelRatio;
setState(() {});
}
}
});
}