Skip to content

Commit

Permalink
feature/strictscale: add possibility to restrict user scale, fix issue
Browse files Browse the repository at this point in the history
…bluefireteam#85 (bluefireteam#537)

Actual behavior : 
User can scale image in or out. When user release scale gesture, app
will rescale if current user scale choice is behind defined limit
(maxScale and minScale).

Proposition : 
A new property to prevent the user to scale behind theses limit.

Co-authored-by: Enguerrand_ARMINJON_MAC_2 <[email protected]>
  • Loading branch information
EArminjon and EArminjon authored May 17, 2023
1 parent b197458 commit d2865d3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/photo_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ class PhotoView extends StatefulWidget {
this.disableGestures,
this.errorBuilder,
this.enablePanAlways,
this.strictScale,
}) : child = null,
childSize = null,
super(key: key);
Expand Down Expand Up @@ -297,6 +298,7 @@ class PhotoView extends StatefulWidget {
this.filterQuality,
this.disableGestures,
this.enablePanAlways,
this.strictScale,
}) : errorBuilder = null,
imageProvider = null,
semanticLabel = null,
Expand Down Expand Up @@ -410,6 +412,9 @@ class PhotoView extends StatefulWidget {
/// Useful when you want to drag a widget without restrictions.
final bool? enablePanAlways;

/// Enable strictScale will restrict user scale gesture to the maxScale and minScale values.
final bool? strictScale;

bool get _isCustomChild {
return child != null;
}
Expand Down Expand Up @@ -530,6 +535,7 @@ class _PhotoViewState extends State<PhotoView>
filterQuality: widget.filterQuality,
disableGestures: widget.disableGestures,
enablePanAlways: widget.enablePanAlways,
strictScale: widget.strictScale,
)
: ImageWrapper(
imageProvider: widget.imageProvider!,
Expand Down Expand Up @@ -557,6 +563,7 @@ class _PhotoViewState extends State<PhotoView>
disableGestures: widget.disableGestures,
errorBuilder: widget.errorBuilder,
enablePanAlways: widget.enablePanAlways,
strictScale: widget.strictScale,
);
},
);
Expand Down
8 changes: 8 additions & 0 deletions lib/src/core/photo_view_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class PhotoViewCore extends StatefulWidget {
required this.filterQuality,
required this.disableGestures,
required this.enablePanAlways,
required this.strictScale,
}) : customChild = null,
super(key: key);

Expand All @@ -64,6 +65,7 @@ class PhotoViewCore extends StatefulWidget {
required this.filterQuality,
required this.disableGestures,
required this.enablePanAlways,
required this.strictScale,
}) : imageProvider = null,
semanticLabel = null,
gaplessPlayback = false,
Expand Down Expand Up @@ -91,6 +93,7 @@ class PhotoViewCore extends StatefulWidget {
final bool tightMode;
final bool disableGestures;
final bool enablePanAlways;
final bool strictScale;

final FilterQuality filterQuality;

Expand Down Expand Up @@ -150,6 +153,11 @@ class PhotoViewCoreState extends State<PhotoViewCore>
final double newScale = _scaleBefore! * details.scale;
final Offset delta = details.focalPoint - _normalizedPosition!;

if (widget.strictScale && (newScale > widget.scaleBoundaries.maxScale ||
newScale < widget.scaleBoundaries.minScale)) {
return;
}

updateScaleStateFromNewScale(newScale);

updateMultiple(
Expand Down
6 changes: 6 additions & 0 deletions lib/src/photo_view_wrappers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ImageWrapper extends StatefulWidget {
required this.disableGestures,
required this.errorBuilder,
required this.enablePanAlways,
required this.strictScale,
}) : super(key: key);

final ImageProvider imageProvider;
Expand Down Expand Up @@ -60,6 +61,7 @@ class ImageWrapper extends StatefulWidget {
final FilterQuality? filterQuality;
final bool? disableGestures;
final bool? enablePanAlways;
final bool? strictScale;

@override
_ImageWrapperState createState() => _ImageWrapperState();
Expand Down Expand Up @@ -192,6 +194,7 @@ class _ImageWrapperState extends State<ImageWrapper> {
controller: widget.controller,
scaleStateController: widget.scaleStateController,
scaleStateCycle: widget.scaleStateCycle ?? defaultScaleStateCycle,
strictScale: widget.strictScale ?? false,
scaleBoundaries: scaleBoundaries,
onTapUp: widget.onTapUp,
onTapDown: widget.onTapDown,
Expand Down Expand Up @@ -251,6 +254,7 @@ class CustomChildWrapper extends StatelessWidget {
required this.filterQuality,
required this.disableGestures,
required this.enablePanAlways,
required this.strictScale,
}) : super(key: key);

final Widget? child;
Expand Down Expand Up @@ -278,6 +282,7 @@ class CustomChildWrapper extends StatelessWidget {
final FilterQuality? filterQuality;
final bool? disableGestures;
final bool? enablePanAlways;
final bool? strictScale;

@override
Widget build(BuildContext context) {
Expand All @@ -299,6 +304,7 @@ class CustomChildWrapper extends StatelessWidget {
scaleStateCycle: scaleStateCycle ?? defaultScaleStateCycle,
basePosition: basePosition ?? Alignment.center,
scaleBoundaries: scaleBoundaries,
strictScale: strictScale ?? false,
onTapUp: onTapUp,
onTapDown: onTapDown,
onScaleEnd: onScaleEnd,
Expand Down

0 comments on commit d2865d3

Please sign in to comment.