Skip to content

Commit

Permalink
fix scaling bugs (first double tap not working, sometimes broken scal…
Browse files Browse the repository at this point in the history
…e after a restate)
  • Loading branch information
NANI-SORE committed Nov 19, 2023
1 parent ece0128 commit 233a392
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
9 changes: 6 additions & 3 deletions lib/photo_view_gallery.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import 'package:photo_view/src/utils/photo_view_hero_attributes.dart';
typedef PhotoViewGalleryPageChangedCallback = void Function(int index);

/// A type definition for a [Function] that defines a page in [PhotoViewGallery.build]
typedef PhotoViewGalleryBuilder = PhotoViewGalleryPageOptions Function(BuildContext context, int index);
typedef PhotoViewGalleryBuilder = PhotoViewGalleryPageOptions Function(
BuildContext context, int index);

/// A [StatefulWidget] that shows multiple [PhotoView] widgets in a [PageView]
///
Expand Down Expand Up @@ -208,7 +209,8 @@ class PhotoViewGallery extends StatefulWidget {
}

class _PhotoViewGalleryState extends State<PhotoViewGallery> {
late final PageController _controller = widget.pageController ?? PageController();
late final PageController _controller =
widget.pageController ?? PageController();

void scaleStateChangedCallback(PhotoViewScaleState scaleState) {
if (widget.scaleStateChangedCallback != null) {
Expand Down Expand Up @@ -314,7 +316,8 @@ class _PhotoViewGalleryState extends State<PhotoViewGallery> {
);
}

PhotoViewGalleryPageOptions _buildPageOption(BuildContext context, int index) {
PhotoViewGalleryPageOptions _buildPageOption(
BuildContext context, int index) {
if (widget._isBuilder) {
return widget.builder!(context, index);
}
Expand Down
15 changes: 9 additions & 6 deletions lib/src/controller/photo_view_controller_delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mixin PhotoViewControllerDelegate on State<PhotoViewCore> {

ScaleBoundaries get scaleBoundaries => widget.scaleBoundaries;

ScaleStateCycle? get scaleStateCycle => widget.scaleStateCycle;
ScaleStateCycle get scaleStateCycle => widget.scaleStateCycle;

Alignment get basePosition => widget.basePosition;
Function(double prevScale, double nextScale)? _animateScale;
Expand Down Expand Up @@ -68,10 +68,13 @@ mixin PhotoViewControllerDelegate on State<PhotoViewCore> {
if (controller.scale == controller.prevValue.scale) {
return;
}

final PhotoViewScaleState newScaleState =
(scale > scaleBoundaries.initialScale)
? PhotoViewScaleState.zoomedIn
: PhotoViewScaleState.zoomedOut;
(scale == scaleBoundaries.initialScale)
? PhotoViewScaleState.initial
: (scale > scaleBoundaries.initialScale)
? PhotoViewScaleState.zoomedIn
: PhotoViewScaleState.zoomedOut;

scaleStateController.setInvisibly(newScaleState);
}
Expand Down Expand Up @@ -126,7 +129,7 @@ mixin PhotoViewControllerDelegate on State<PhotoViewCore> {
final PhotoViewScaleState scaleState = scaleStateController.scaleState;
if (scaleState == PhotoViewScaleState.zoomedIn ||
scaleState == PhotoViewScaleState.zoomedOut) {
scaleStateController.scaleState = scaleStateCycle!(scaleState);
scaleStateController.scaleState = scaleStateCycle(scaleState);
return;
}
final double originalScale = getScaleForScaleState(
Expand All @@ -142,7 +145,7 @@ mixin PhotoViewControllerDelegate on State<PhotoViewCore> {
do {
prevScale = nextScale;
prevScaleState = nextScaleState;
nextScaleState = scaleStateCycle!(prevScaleState);
nextScaleState = scaleStateCycle(prevScaleState);
nextScale = getScaleForScaleState(nextScaleState, scaleBoundaries);
} while (prevScale == nextScale && scaleState != nextScaleState);

Expand Down
14 changes: 6 additions & 8 deletions lib/src/core/photo_view_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class PhotoViewCore extends StatefulWidget {
required this.gestureDetectorBehavior,
required this.controller,
required this.scaleBoundaries,
this.scaleStateCycle,
required this.scaleStateCycle,
required this.scaleStateController,
required this.basePosition,
required this.tightMode,
Expand All @@ -63,7 +63,7 @@ class PhotoViewCore extends StatefulWidget {
this.gestureDetectorBehavior,
required this.controller,
required this.scaleBoundaries,
this.scaleStateCycle,
required this.scaleStateCycle,
required this.scaleStateController,
required this.basePosition,
required this.tightMode,
Expand All @@ -87,7 +87,7 @@ class PhotoViewCore extends StatefulWidget {
final PhotoViewControllerBase controller;
final PhotoViewScaleStateController scaleStateController;
final ScaleBoundaries scaleBoundaries;
final ScaleStateCycle? scaleStateCycle;
final ScaleStateCycle scaleStateCycle;
final Alignment basePosition;

final PhotoViewImageTapUpCallback? onTapUp;
Expand Down Expand Up @@ -440,11 +440,9 @@ class PhotoViewCoreState extends State<PhotoViewCore>

return PhotoViewGestureDetector(
child: child,
onDoubleTapDown:
widget.scaleStateCycle != null ? recordPointerPosition : null,
onDoubleTap: widget.scaleStateCycle != null ? onDoubleTap : null,
onDoubleTapCancel:
widget.scaleStateCycle != null ? clearPointerPosition : null,
onDoubleTapDown: recordPointerPosition,
onDoubleTap: onDoubleTap,
onDoubleTapCancel: clearPointerPosition,
onScaleStart: onScaleStart,
onScaleUpdate: onScaleUpdate,
onScaleEnd: onScaleEnd,
Expand Down
5 changes: 2 additions & 3 deletions lib/src/core/photo_view_gesture_detector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,8 @@ class DoubleTapAndTapDragZoomGestureRecognizer
}

// If second tap is not allowed, reset the state.
final bool isPointerAllowed =
supportedDevices == null || supportedDevices!.contains(event.kind);
if (isPointerAllowed == false) {
final bool isPointerAllowed = super.isPointerAllowed(event);
if (!isPointerAllowed) {
_reset();
}
return isPointerAllowed;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/utils/photo_view_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ double getScaleForScaleState(
);
case PhotoViewScaleState.zoomedIn:
return _clampSize(
scaleBoundaries.initialScale, // maxScale
scaleBoundaries.maxScale,
scaleBoundaries,
);
case PhotoViewScaleState.zoomedOut:
return _clampSize(
scaleBoundaries.initialScale, // minScale
scaleBoundaries.minScale,
scaleBoundaries,
);
case PhotoViewScaleState.covering:
Expand Down

0 comments on commit 233a392

Please sign in to comment.