gpt4 book ai didi

flutter - Flutter:如何使用matrix_gesture_detector设置最大/最小刻度

转载 作者:行者123 更新时间:2023-12-03 03:23:13 27 4
gpt4 key购买 nike

我正在尝试使列缩放/可移动。但是,如何设置最大变焦和最小变焦呢?这样就不会放大到无限远。

现在使用此方法:

 Matrix4 matrix = Matrix4.identity();

MatrixGestureDetector(
shouldRotate: false
onMatrixUpdate: (Matrix4 m, Matrix4 tm, Matrix4 sm, Matrix4 rm) {
setState(() {
matrix = m;
});
},
child: Transform(
transform: matrix,
child: Column(
),
),

最佳答案

我遇到了同样的问题,花了一段时间,但遇到了解决方案。

在Flutter的 Transform.scale source中进行了一些挖掘之后,揭示了这一行,这给了我们提示:

transform = Matrix4.diagonal3Values(scale, scale, 1.0)

它使用的是您在 Matrix4中收到的 onMatrixUpdate中的对角线值。因此,它从第一个Vector4中获取 x,从第二个中获取 y,从第三个中获取 z。 (据我所知,第四项是固定的)。因此,这些是您需要限制的值。在此示例中,我制作了一个小的 _minMax方法,该方法在相关时将比例尺限制在相关的最小/最大值(可以将其传递给 null来忽略限制的任一侧)。

我用这个来限制规模:
typedef MathF<T extends num> = T Function(T, T);
typedef VFn = Vector4 Function(double x, double y, double z, double w);

double _minMax(num _min, num _max, num actual) {
if (_min == null && _max == null) {
return actual.toDouble();
}

if (_min == null) {
return min(_max.toDouble(), actual.toDouble());
}

if (_max == null) {
return max(_min.toDouble(), actual.toDouble());
}

return min(_max.toDouble(), max(_min.toDouble(), actual.toDouble()));
}

// ... ... ...

onMatrixUpdate: (Matrix4 m, Matrix4 tm, Matrix4 sm, Matrix4 rm) {
var finalM = Matrix4.copy(m);
Map<int, VFn> colmap = {
0: (x, y, z, w) {
x = _minMax(widget.minScale, widget.maxScale, x);
return Vector4(x, y, z, w);
},
1: (x, y, z, w) {
y = _minMax(widget.minScale, widget.maxScale, y);
return Vector4(x, y, z, w);
},
2: (x, y, z, w) {
z = _minMax(widget.minScale, widget.maxScale, z);
return Vector4(x, y, z, w);
},
};
for (var col in colmap.keys) {
var oldCol = m.getColumn(col);
var colD = colmap[col];
if (colD != null) {
finalM.setColumn(col, colD(oldCol.x, oldCol.y, oldCol.z, oldCol.w));
}
}
setState(() {
matrix = finalM;
});
},

关于flutter - Flutter:如何使用matrix_gesture_detector设置最大/最小刻度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60858334/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com