Axel
Axel

Reputation: 11

How to reset scale of matrix in Matrix Gesture Detector widget in flutter?

I am using the Matrix Gesture Detector widget in the flutter. I managed to do reset the scaling but when next time I zoom in it starts from the last zoomed position. I can't find a solution to fix that. Can someone please tell me a way to start from the original position Here is my code

import 'package:flutter/material.dart';
import 'package:matrix_gesture_detector/matrix_gesture_detector.dart';

class ZoomableWidget extends StatefulWidget {
  final Widget child;

  const ZoomableWidget({Key key, this.child}) : super(key: key);
  @override
  _ZoomableWidgetState createState() => _ZoomableWidgetState();
}

class _ZoomableWidgetState extends State<ZoomableWidget> {
  Matrix4 matrix = Matrix4.identity();
  Matrix4 resetMatrix =  Matrix4.identity();

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onDoubleTap: (){
        setState(() {
          matrix = resetMatrix;
        });
      },
      child: MatrixGestureDetector(
        shouldRotate: false,
        onMatrixUpdate: (Matrix4 m, Matrix4 tm, Matrix4 sm, Matrix4 rm) {
          setState(() {
            matrix = m;
          });
        },
        child: Transform(
          transform: matrix,
          child: widget.child,
        ),
      ),
    );
  }
}

Upvotes: 1

Views: 866

Answers (2)

Makayasa
Makayasa

Reputation: 51

I got this problem too, but I got the solution. First you need a callback for scaleEnd. And then you need to pass all the matrix from the last value from onMatrixUpdate to the widget. And then you need some boolean in onScaleStart to change the matrix to your lat

 void onScaleStart(ScaleStartDetails details) {
if (widget.mMatrix != null) {
  matrix = MxGestureDetector.compose(
    widget.mMatrix!,
    widget.tMatrix!,
    widget.sMatrix!,
    widget.rMatrix!,
  );
}
translationUpdater.value = details.focalPoint;
scaleUpdater.value = 1.0;
rotationUpdater.value = 0.0;

}

Upvotes: 0

You need to fix your onMatrixUpdate method:

onMatrixUpdate: (Matrix4 m, Matrix4 tm, Matrix4 sm, Matrix4 rm) {
  setState(() {
    matrix = MatrixGestureDetector.compose(matrix, tm, sm, rm);
  });
},

Upvotes: 1

Related Questions