From c5d14a0feaf5a1c69c03fbd6bfdc050f281d76d3 Mon Sep 17 00:00:00 2001 From: l Date: Sat, 10 Jul 2021 03:20:58 +0430 Subject: [PATCH 1/2] Add zoom limit for scaling. --- lib/src/crop.dart | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/src/crop.dart b/lib/src/crop.dart index 3f2dc0f..9904b99 100644 --- a/lib/src/crop.dart +++ b/lib/src/crop.dart @@ -71,11 +71,13 @@ class _CropState extends State with TickerProviderStateMixin { final _repaintBoundaryKey = GlobalKey(); double _previousScale = 1; + double scaleLimit = 3; //default value 3 Offset _previousOffset = Offset.zero; Offset _startOffset = Offset.zero; Offset _endOffset = Offset.zero; double _previousGestureRotation = 0.0; + set scaleLimitSetter(scale) => scaleLimit = scale; /// Store the pointer count (finger involved to perform scaling). /// /// This is used to compare with the value in @@ -191,7 +193,9 @@ class _CropState extends State with TickerProviderStateMixin { void _onScaleUpdate(ScaleUpdateDetails details) { widget.controller._offset += details.focalPoint - _previousOffset; _previousOffset = details.focalPoint; - widget.controller._scale = _previousScale * details.scale; + if(_previousScale < scaleLimit){ + widget.controller._scale = _previousScale * details.scale; + } _startOffset = widget.controller._offset; _endOffset = widget.controller._offset; From a39a0b818df581b7d1438ae6858500060f0cb9e6 Mon Sep 17 00:00:00 2001 From: l Date: Sat, 10 Jul 2021 21:47:49 +0430 Subject: [PATCH 2/2] improvement in zoom constraint design --- example/lib/main.dart | 1 + lib/src/crop.dart | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 518dda9..1edcad2 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -138,6 +138,7 @@ class _MyHomePageState extends State { ), ) : null, + scaleLimit: 3.0, ), ), ), diff --git a/lib/src/crop.dart b/lib/src/crop.dart index 9904b99..c495998 100644 --- a/lib/src/crop.dart +++ b/lib/src/crop.dart @@ -23,6 +23,7 @@ class Crop extends StatefulWidget { final BoxShape shape; final ValueChanged? onChanged; final Duration animationDuration; + final double? scaleLimit; const Crop({ Key? key, @@ -38,6 +39,7 @@ class Crop extends StatefulWidget { this.interactive = true, this.shape = BoxShape.rectangle, this.onChanged, + this.scaleLimit, this.animationDuration = const Duration(milliseconds: 200), }) : super(key: key); @@ -71,13 +73,13 @@ class _CropState extends State with TickerProviderStateMixin { final _repaintBoundaryKey = GlobalKey(); double _previousScale = 1; - double scaleLimit = 3; //default value 3 + Offset _previousOffset = Offset.zero; Offset _startOffset = Offset.zero; Offset _endOffset = Offset.zero; double _previousGestureRotation = 0.0; - set scaleLimitSetter(scale) => scaleLimit = scale; + /// Store the pointer count (finger involved to perform scaling). /// /// This is used to compare with the value in @@ -193,8 +195,9 @@ class _CropState extends State with TickerProviderStateMixin { void _onScaleUpdate(ScaleUpdateDetails details) { widget.controller._offset += details.focalPoint - _previousOffset; _previousOffset = details.focalPoint; - if(_previousScale < scaleLimit){ - widget.controller._scale = _previousScale * details.scale; + widget.controller._scale = _previousScale * details.scale; + if(widget.scaleLimit != null && widget.controller._scale > widget.scaleLimit!){ + widget.controller._scale = widget.scaleLimit!; } _startOffset = widget.controller._offset; _endOffset = widget.controller._offset;