toScene method Null safety

Offset toScene(
  1. Offset viewportPoint
)

Return the scene point at the given viewport point.

A viewport point is relative to the parent while a scene point is relative to the child, regardless of transformation. Calling toScene with a viewport point essentially returns the scene coordinate that lies underneath the viewport point given the transform.

The viewport transforms as the inverse of the child (i.e. moving the child left is equivalent to moving the viewport right).

This method is often useful when determining where an event on the parent occurs on the child. This example shows how to determine where a tap on the parent occurred on the child.

@override
void build(BuildContext context) {
  return GestureDetector(
    onTapUp: (TapUpDetails details) {
      _childWasTappedAt = _transformationController.toScene(
        details.localPosition,
      );
    },
    child: InteractiveViewer(
      transformationController: _transformationController,
      child: child,
    ),
  );
}

Implementation

Offset toScene(Offset viewportPoint) {
  // On viewportPoint, perform the inverse transformation of the scene to get
  // where the point would be in the scene before the transformation.
  final Matrix4 inverseMatrix = Matrix4.inverted(value);
  final Vector3 untransformed = inverseMatrix.transform3(Vector3(
    viewportPoint.dx,
    viewportPoint.dy,
    0,
  ));
  return Offset(untransformed.x, untransformed.y);
}