Reputation: 1349
I have a MatrixGestureDetector which is responsible for zooming and moving my displayed image. When I close the app I save the _notifier.value to Preferences and load it on restart of the app, so far so good, the image is shown exactly in the way it was before the restart.
But as soon as I try to zoom again by gesture pinch, the image suddenly unexpectly resets to an identity matrix. What is wrong? It looks to me as if I forgot to save, load and apply another important setting.
...
child: Center(
child: MatrixGestureDetector(
onMatrixUpdate: (m, tm, sm, rm) {
//print(m);
_notifier.value = m;
},
child: AnimatedBuilder(
animation: _notifier,
builder: (ctx, child) {
return Transform(
transform: _notifier.value,
child: Center(
child: Container(
child: Transform.scale(
scale: 1,
origin: Offset(_image_offset_x, _image_offset_y),
child: _image,
),
...
Upvotes: 0
Views: 145
Reputation: 4499
You need to separate the initial Matrix (last save) and the current matrix (this run) because the matrix inside MatrixGestureDetector
, onMatrixUpdate
always start from Matrix4.identity()
// your _notifier (matrix value on this run)
final _notifier = ValueNotifier<Matrix4>(Matrix4.identity());
// load matrix from shared preference
final initialMatrix = await loadMatrix();
// when you try to save the matrix, multiplicate them
await saveMatrix(_notifier.value * initialMatrix);
And you can add the initial matrix to the Transform
...
return Transform(
transform: _notifier.value * initialMatrix,
child: Center(
...
Upvotes: 1