-
-
Notifications
You must be signed in to change notification settings - Fork 494
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
getCropRect() returns Rect with negative values #580
Comments
is there any cases or demo to reproduce it? |
Source codeimport "dart:io";
import "package:flutter/material.dart";
import "package:flutter/services.dart";
import "package:image_editor/image_editor.dart";
import "package:extended_image/extended_image.dart";
// dependencies:
// extended_image: 7.0.2
// image_editor: 1.3.0
// 1. Make sure to remove "com.apple.security.app-sandbox" from entitlements
// 2. Prepare "1.png" file
// 3. Click F3 to crop image, and ENTER to save the file
// 4. Try several times!
void main(List<String> args) async {
runApp(MaterialApp(home: Scaffold(body: MyApp(File("/Users/tommy/1.png")))));
}
class MyApp extends StatefulWidget {
final File startPath;
const MyApp(this.startPath);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final editorKey = GlobalKey<ExtendedImageEditorState>();
final extImgKey = GlobalKey();
ExtendedImageMode _mode = ExtendedImageMode.gesture;
@override
Widget build(BuildContext context) {
return Shortcuts(
shortcuts: {
const SingleActivator(LogicalKeyboardKey.enter): SaveFileIntent(),
const SingleActivator(LogicalKeyboardKey.f3): SwitchModeIntent(),
},
child: Actions(
actions: {
SaveFileIntent: CallbackAction(onInvoke: (_) => _saveFile()),
SwitchModeIntent: CallbackAction(onInvoke: (_) => _switchMode()),
},
child: Focus(
autofocus: true,
child: ExtendedImage.file (key: extImgKey, widget.startPath, mode: _mode, fit: _mode == ExtendedImageMode.editor ? BoxFit.contain : null, width: double.infinity, height: double.infinity, extendedImageEditorKey: editorKey, cacheRawData: true)
)
)
);
}
void _switchMode() {
setState(() {
_mode = _mode == ExtendedImageMode.editor ? ExtendedImageMode.gesture : ExtendedImageMode.editor;
});
}
void _saveFile() {
converterMacOs(Uint8List image, Rect? cropRect) async {
final option = ImageEditorOption();
if (cropRect != null) option.addOption(ClipOption(x: cropRect.left, y: cropRect.top, width: cropRect.width, height: cropRect.height));
final result = await ImageEditor.editImage(image: image, imageEditorOption: option);
return result!;
}
_saveFileInternal(converterMacOs);
}
void _saveFileInternal(ImageConverter converter) async {
Rect? cropOption;
switch (_mode) {
case ExtendedImageMode.editor:
final state = editorKey.currentState!;
final action = state.editAction!;
final cropRect = state.getCropRect()!;
print("Rect is $cropRect");
if (action.needCrop) {
cropOption = cropRect;
}
break;
default:
}
if (cropOption != null) {
final exImage = extImgKey.currentWidget as ExtendedImage;
final imageProvider = exImage.image as ExtendedFileImageProvider;
final Uint8List image = imageProvider.rawImageData;
final Uint8List bytes = await converter.call(image, cropOption);
widget.startPath.writeAsBytesSync(bytes, flush: true);
clearMemoryImageCache();
_switchMode();
}
}
}
typedef ImageConverter = Future<Uint8List> Function(Uint8List curImage, Rect? cropRect);
class SaveFileIntent extends Intent {}
class SwitchModeIntent extends Intent {}
How to reproduce (press F3 and ENTER): |
i can't reprduce it at my side, can you try it on latest version of extended_image? |
@zmtzawqlp, I ended up with this dirty-hack method: Rect _fixRect(Rect rect) {
// extended_image: 7.0.2 has a bug when sometimes it provides "-0.0" values in Rect
if (rect.left.isNegative || rect.right.isNegative || rect.top.isNegative || rect.bottom.isNegative) {
final left = rect.left.isNegative ? 0.0 : rect.left;
final right = rect.right.isNegative ? 0.0 : rect.right;
final top = rect.top.isNegative ? 0.0 : rect.top;
final bottom = rect.bottom.isNegative ? 0.0 : rect.bottom;
return Rect.fromLTRB(left, top, right, bottom);
}
return rect;
} which works for me. Will try to check 8.2.0 at my spare time |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Version
7.0.2
Platforms
macOS
Device Model
MacOS 13.3.1
flutter info
How to reproduce?
Logs
Example code (optional)
No response
Contact
mitrakov-artem@yandex.ru
The text was updated successfully, but these errors were encountered: