如何保存和检索颤振Vector_math库中的 Matrix4 对象?



我有用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个双打的列表。它也有命名的构造函数(.fromList.fromFloat64List(以及普通构造函数(需要16个单独的双打(,它将从其组成部分构造一个Matrix4

根据您希望如何在SQLite中存储数据,您可以使用这些组合。如果要将所有 16 个双精度值存储为数据库中的列,请使用 storage[0], storage[1], ... 作为列值。您可能还希望将其存储为 16 个值的字符分隔字符串。您可以打印将所有 16 个值附加List.join(' ')并使用 String.split(' ') 解析它们。

最有效的方法(但人类可读性最低(可能是将其存储为 128 字节的 BLOB。使用 matrix.storage.buffer.asUint8List()matrix转换为字节,并使用 Matrix4.fromBuffer(bytes.buffer, 0) 从名为 bytesUint8List构造矩阵。

最新更新