我有使用 Matrix4 初始化的 ValueNotifier。我可以改变我的看法。现在我想以某种方式在 SQLite 中保存 ValueNotifier 的当前值,并在加载时再次使用保存的 Matrix4 值初始化我的 ValueNotifier。下面是代码:
ValueNotifier<Matrix4> notifier = ValueNotifier(Matrix4.identity());
MatrixGestureDetector(
onMatrixUpdate: (matrix, translationMatrix, scaleMatrix, rotationMatrix) {
notifier.value = matrix;
},
child: AnimatedBuilder(animation: notifier,
builder: (context, child) {
return Transform(
transform: notifier.value,
child: Container(
width: width,
height: height,
color: Colors.yellow,
),
);
}),
)
最佳答案
Matrix4
有一个 getter storage
,它返回一个包含 16 个 double 的列表。它还具有命名构造函数(.fromList
和 .fromFloat64List
)以及将构造一个 Matrix4
的普通构造函数(需要 16 个单独的 double ) > 从它的组成部分回来。
根据您希望如何在 SQLite 中存储数据,您可以组合使用这些方法。如果您想将所有 16 个 double 作为列存储在数据库中,请使用 storage[0]、storage[1]、...
作为您的列值。您可能还想将其存储为由 16 个值组成的以字符分隔的字符串。您可以使用 List.join(' ')
打印附加所有 16 个值,然后使用 String.split(' ')
解析它们。
最有效的方法(但人类可读性最低)可能是将其存储为 128 字节的 BLOB。使用matrix.storage.buffer.asUint8List()
将matrix
转换为字节,使用Matrix4.fromBuffer(bytes.buffer, 0)
将从名为 bytes
的 Uint8List
构造一个矩阵。
关于matrix - 如何保存和检索 flutter 的 Vector_math 库的 Matrix4 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55324644/