如何保存和检索 Flutter 的 Vector_math 库的 Matrix4 对象?

Ank*_*ash 0 matrix save flutter

我有用 Matrix4 初始化的 ValueNotifier。我可以改变我的看法。现在我想以某种方式将 ValueNotifier 的当前值保存在 SQLite 中,并再次在加载时使用保存的 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,

                      ),
                    );
                  }),
            )
Run Code Online (Sandbox Code Playgroud)

Ric*_*eap 5

Matrix4有一个storage返回 16 个双精度列表的getter 。它还具有命名构造函数 ( .fromListand .fromFloat64List) 以及普通构造函数(需要 16 个单独的双打),它将Matrix4从其组成部分构造一个back 。

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

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