Change RenderObject.getTransformTo to include ancestor. (#37652)

getTransformTo now includes ancestor in the transform it returns, except
for if ancestor is the root view, ensuring that the transform remains in
logical pixel space.
This commit is contained in:
Tom Robinson
2019-08-15 20:52:15 -04:00
committed by GitHub
parent 1df165ea13
commit 0b3f2f61a2
4 changed files with 16 additions and 2 deletions

View File

@@ -1002,7 +1002,7 @@ class RenderListWheelViewport
final ListWheelParentData parentData = child.parentData;
final double targetOffset = parentData.offset.dy; // the so-called "centerPosition"
final Matrix4 transform = target.getTransformTo(this);
final Matrix4 transform = target.getTransformTo(child);
final Rect bounds = MatrixUtils.transformRect(transform, rect);
final Rect targetRect = bounds.translate(0.0, (size.height - itemExtent) / 2);

View File

@@ -2171,6 +2171,7 @@ abstract class RenderObject extends AbstractNode with DiagnosticableTreeMixin im
/// logical pixels. To get physical pixels, use [applyPaintTransform] from the
/// [RenderView] to further transform the coordinate.
Matrix4 getTransformTo(RenderObject ancestor) {
final bool ancestorSpecified = ancestor != null;
assert(attached);
if (ancestor == null) {
final AbstractNode rootNode = owner.rootNode;
@@ -2182,6 +2183,8 @@ abstract class RenderObject extends AbstractNode with DiagnosticableTreeMixin im
assert(renderer != null); // Failed to find ancestor in parent chain.
renderers.add(renderer);
}
if (ancestorSpecified)
renderers.add(ancestor);
final Matrix4 transform = Matrix4.identity();
for (int index = renderers.length - 1; index > 0; index -= 1) {
renderers[index].applyPaintTransform(renderers[index - 1], transform);

View File

@@ -576,7 +576,7 @@ class _RenderSingleChildViewport extends RenderBox with RenderObjectWithChildMix
return RevealedOffset(offset: offset.pixels, rect: rect);
final RenderBox targetBox = target;
final Matrix4 transform = targetBox.getTransformTo(this);
final Matrix4 transform = targetBox.getTransformTo(child);
final Rect bounds = MatrixUtils.transformRect(transform, rect);
final Size contentSize = child.size;

View File

@@ -995,6 +995,17 @@ void main() {
);
}
});
test('localToGlobal with ancestor', () {
final RenderConstrainedBox innerConstrained = RenderConstrainedBox(additionalConstraints: const BoxConstraints.tightFor(width: 50, height: 50));
final RenderPositionedBox innerCenter = RenderPositionedBox(alignment: Alignment.center, child: innerConstrained);
final RenderConstrainedBox outerConstrained = RenderConstrainedBox(additionalConstraints: const BoxConstraints.tightFor(width: 100, height: 100), child: innerCenter);
final RenderPositionedBox outerCentered = RenderPositionedBox(alignment: Alignment.center, child: outerConstrained);
layout(outerCentered);
expect(innerConstrained.localToGlobal(Offset.zero, ancestor: outerConstrained).dy, 25.0);
});
});
}