calculatePageMatrix method
Calculate the matrix that corresponding to the page of specified offset (x
, y
) and specified zoomRatio
.
x
,y
should be in 0 1
range and they indicate relative position in the page:
- 0 for top/left
- 1 for bottom/right
- 0.5 for center (the default)
anchor
specifies which widget corner, edge, or center the point specified by (x
,y
) is anchored to.
zoomRatio
specifies the zoom ratio. The default is to use the zoom ratio that fit the page into the view.
If you want to keep the current zoom ratio, use PdfViewerController.zoomRatio for the value.
If the page does not exist in the layout, it returns null. If the controller is not ready(isReady), the method throws some exception.
Implementation
Matrix4? calculatePageMatrix({
required int pageNumber,
double? padding,
double x = 0.5,
double y = 0.5,
PdfViewerAnchor anchor = PdfViewerAnchor.center,
double? zoomRatio,
}) {
final rect = getPageRect(pageNumber)?.inflate(padding ?? _state!._padding);
if (rect == null) return null;
final zoom1 = _state!._lastViewSize!.width / rect.width;
final destZoom = zoomRatio ?? zoom1;
final ratio = destZoom / zoom1;
final viewWidth = _state!._lastViewSize!.width;
final viewHeight = _state!._lastViewSize!.height;
final left = clampX(
(rect.left + rect.width * x) * ratio -
viewWidth * (anchor.index % 3) / 2,
destZoom);
final top = clampY(
(rect.top + rect.height * y) * ratio -
viewHeight * (anchor.index ~/ 3) / 2,
destZoom);
return Matrix4.compose(
math64.Vector3(-left, -top, 0),
math64.Quaternion.identity(),
math64.Vector3(destZoom, destZoom, 1),
);
}