如何创建一个可缩放的窗口小部件,当捏合结束时,缩放的窗口小部件将获得其初始位置?

时间:2019-08-21 09:38:38

标签: flutter dart

我想使用Matrix_Gesture_Detector包创建一个Zoomable小部件。我用以下代码创建了小部件:

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:matrix_gesture_detector/matrix_gesture_detector.dart';
import 'package:vector_math/vector_math_64.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 defaultMatrix = Matrix4.diagonal3(
      Vector3(1.0.clamp(1.0, 5.0), 1.0.clamp(1.0, 5.0), 1.0.clamp(1.0, 5.0)));
  Matrix4 matrix;
  @override
  void initState() {
    matrix = defaultMatrix;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return ClipRect(
      child: MatrixGestureDetector(
        shouldTranslate: false,clipChild: true,focalPointAlignment: Alignment.center,
        shouldRotate: false,
        onMatrixUpdate: (Matrix4 m, Matrix4 tm, Matrix4 sm, Matrix4 rm) {
          setState(() {
            matrix = m;
          });
        },
        child: Transform(
          transform: matrix,
          alignment: FractionalOffset.center,
          child: widget.child,
        ),
      ),
    );
  }
}

现在我想要的是,当用户释放屏幕(捏合结束)时,小部件将返回其初始位置。任何想法我怎么能做到这一点? 还有如何防止缩放缩小而使容器比其原始尺寸小?

0 个答案:

没有答案