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