Skip to content

Commit

Permalink
add argument to onMoved
Browse files Browse the repository at this point in the history
  • Loading branch information
chooyan-eng committed Jul 18, 2024
1 parent 7dc1289 commit 6128a4f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
24 changes: 20 additions & 4 deletions lib/src/widget/crop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ typedef CornerDotBuilder = Widget Function(

typedef CroppingRectBuilder = ViewportBasedRect Function(
ViewportBasedRect viewportRect,
ViewportBasedRect imageRect,
ImageBasedRect imageRect,
);

typedef OnMovedCallback = void Function(
ViewportBasedRect viewportRect,
ImageBasedRect imageRect,
);

enum CropStatus { nothing, loading, ready, cropping }
Expand Down Expand Up @@ -72,7 +77,7 @@ class Crop extends StatelessWidget {
final CropController? controller;

/// Callback called when cropping rect changes for any reasons.
final ValueChanged<ViewportBasedRect>? onMoved;
final OnMovedCallback? onMoved;

/// Callback called when status of Crop widget is changed.
///
Expand Down Expand Up @@ -208,7 +213,7 @@ class _CropEditor extends StatefulWidget {
final ImageBasedRect? initialArea;
final bool withCircleUi;
final CropController? controller;
final ValueChanged<ViewportBasedRect>? onMoved;
final OnMovedCallback? onMoved;
final ValueChanged<CropStatus>? onStatusChanged;
final Color? maskColor;
final Color baseColor;
Expand Down Expand Up @@ -279,7 +284,18 @@ class _CropEditorState extends State<_CropEditor> {
late ViewportBasedRect _cropRect;
set cropRect(ViewportBasedRect newRect) {
setState(() => _cropRect = newRect);
widget.onMoved?.call(_cropRect);

final screenSizeRatio = calculator.screenSizeRatio(
_parsedImageDetail!,
_viewportSize,
);
final imageBaseRect = Rect.fromLTWH(
(_cropRect.left - _imageRect.left) * screenSizeRatio / _scale,
(_cropRect.top - _imageRect.top) * screenSizeRatio / _scale,
_cropRect.width * screenSizeRatio / _scale,
_cropRect.height * screenSizeRatio / _scale,
);
widget.onMoved?.call(_cropRect, imageBaseRect);
}

bool get _isImageLoading => _lastComputed != null;
Expand Down
10 changes: 5 additions & 5 deletions test/widget/controller_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void main() {
image: testLandscapeImage,
controller: controller,
onCropped: (value) {},
onMoved: (rect) {
onMoved: (rect, _) {
initialRect ??= rect;
lastRect = rect;
},
Expand Down Expand Up @@ -126,7 +126,7 @@ void main() {
controller: controller,
aspectRatio: 1,
onCropped: (value) {},
onMoved: (rect) {
onMoved: (rect, _) {
initialRect ??= rect;
lastRect = rect;
},
Expand Down Expand Up @@ -161,7 +161,7 @@ void main() {
controller: controller,
aspectRatio: 1,
onCropped: (value) {},
onMoved: (rect) => lastRect = rect,
onMoved: (rect, _) => lastRect = rect,
),
);

Expand Down Expand Up @@ -195,7 +195,7 @@ void main() {
controller: controller,
aspectRatio: 1,
onCropped: (value) {},
onMoved: (rect) => lastRect = rect,
onMoved: (rect, _) => lastRect = rect,
),
);

Expand Down Expand Up @@ -233,7 +233,7 @@ void main() {
controller: controller,
aspectRatio: 1,
onCropped: (value) {},
onMoved: (rect) => lastRect = rect,
onMoved: (rect, _) => lastRect = rect,
),
);

Expand Down
8 changes: 4 additions & 4 deletions test/widget/crop_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ void main() {
Crop(
image: testImage,
onCropped: (value) {},
onMoved: (value) {
onMoved: (_, __) {
if (!completer.isCompleted) {
completer.complete();
}
Expand Down Expand Up @@ -196,7 +196,7 @@ void main() {
Crop(
image: testImage,
onCropped: (value) {},
onMoved: (value) {
onMoved: (_, __) {
if (!completer.isCompleted) {
completer.complete();
}
Expand Down Expand Up @@ -234,7 +234,7 @@ void main() {
Crop(
image: testImage,
onCropped: (value) {},
onMoved: (value) {
onMoved: (_, __) {
if (!completer.isCompleted) {
completer.complete();
}
Expand Down Expand Up @@ -270,7 +270,7 @@ void main() {
Crop(
image: testImage,
onCropped: (value) {},
onMoved: (value) {
onMoved: (_, __) {
calledCount++;
},
fixCropRect: true,
Expand Down

0 comments on commit 6128a4f

Please sign in to comment.