matrix - 如何保存和检索 flutter 的 Vector_math 库的 Matrix4 对象?

标签 matrix flutter save

我有使用 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)将从名为 bytesUint8List 构造一个矩阵。

关于matrix - 如何保存和检索 flutter 的 Vector_math 库的 Matrix4 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55324644/

相关文章:

django - 在保存时在 Django 的管理面板中操作数据

java mongo 数组列表保存

Java如何提示用户保存word文档

c++ - 将 Matlab 中的以下 Matrix 代码转换为 C++ 中的 Eigen 的问题

flutter - Flutter开发中如何启用spread(...)算子

dart - 如何通过点击 BottomNavigationBarItem 以编程方式打开抽屉?

flutter - 具有命名参数的构造函数设置私有(private)变量后验证

c++ - 矩阵相互覆盖

python - 如何使用 PyTorch 仅对矩阵的上三角元素进行软最大化?

c - 我编写了一个程序来添加两个矩阵,但它不起作用。对出了什么问题有什么建议吗?