fix: pinch zoom in mobile app (#18744)

* Change photo zoom logic

To properly zoom to current position and pan at the correct speed

TODO: zoom to current pinch position

* zoom to current pinch position

* Clean unused variable

* Formatting

* fix: lint

---------

Co-authored-by: Alex <alex.tran1502@gmail.com>
This commit is contained in:
toamz 2025-05-29 16:19:26 +02:00 committed by GitHub
parent 1f18a09061
commit 0e81c20cbb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -120,7 +120,6 @@ class PhotoViewCoreState extends State<PhotoViewCore>
TickerProviderStateMixin, TickerProviderStateMixin,
PhotoViewControllerDelegate, PhotoViewControllerDelegate,
HitCornersDetector { HitCornersDetector {
Offset? _normalizedPosition;
double? _scaleBefore; double? _scaleBefore;
double? _rotationBefore; double? _rotationBefore;
@ -153,23 +152,29 @@ class PhotoViewCoreState extends State<PhotoViewCore>
void onScaleStart(ScaleStartDetails details) { void onScaleStart(ScaleStartDetails details) {
_rotationBefore = controller.rotation; _rotationBefore = controller.rotation;
_scaleBefore = scale; _scaleBefore = scale;
_normalizedPosition = details.focalPoint - controller.position;
_scaleAnimationController.stop(); _scaleAnimationController.stop();
_positionAnimationController.stop(); _positionAnimationController.stop();
_rotationAnimationController.stop(); _rotationAnimationController.stop();
} }
void onScaleUpdate(ScaleUpdateDetails details) { void onScaleUpdate(ScaleUpdateDetails details) {
final centeredFocalPoint = Offset(
details.focalPoint.dx - scaleBoundaries.outerSize.width / 2,
details.focalPoint.dy - scaleBoundaries.outerSize.height / 2,
);
final double newScale = _scaleBefore! * details.scale; final double newScale = _scaleBefore! * details.scale;
final Offset delta = details.focalPoint - _normalizedPosition!; final double scaleDelta = newScale / scale;
final Offset newPosition =
(controller.position + details.focalPointDelta) * scaleDelta -
centeredFocalPoint * (scaleDelta - 1);
updateScaleStateFromNewScale(newScale); updateScaleStateFromNewScale(newScale);
updateMultiple( updateMultiple(
scale: newScale, scale: newScale,
position: widget.enablePanAlways position: widget.enablePanAlways
? delta ? newPosition
: clampPosition(position: delta, scale: details.scale), : clampPosition(position: newPosition),
rotation: rotation:
widget.enableRotation ? _rotationBefore! + details.rotation : null, widget.enableRotation ? _rotationBefore! + details.rotation : null,
rotationFocusPoint: widget.enableRotation ? details.focalPoint : null, rotationFocusPoint: widget.enableRotation ? details.focalPoint : null,