Parent data not updated when reparenting using global keys

When we reactivate a subtree that had a global key, we weren't updating the
parent data because:

1) The child wasn't in the tree when we updated the parent data element.
2) The activated child didn't go through mount (just through update).

This patch moves the parent data update work to when we attach the render
object, which we do both during mount and when reactivating a child.

Fixes #345
This commit is contained in:
Adam Barth
2015-11-12 16:51:34 -08:00
parent 05d94b5a9f
commit 35464f447b
2 changed files with 54 additions and 3 deletions

View File

@@ -1275,9 +1275,6 @@ abstract class RenderObjectElement<T extends RenderObjectWidget> extends Buildab
super.mount(parent, newSlot);
assert(_slot == newSlot);
attachRenderObject(newSlot);
ParentDataElement parentDataElement = _findAncestorParentDataElement();
if (parentDataElement != null)
updateParentData(parentDataElement.widget);
_dirty = false;
}
@@ -1491,6 +1488,9 @@ abstract class RenderObjectElement<T extends RenderObjectWidget> extends Buildab
_slot = newSlot;
_ancestorRenderObjectElement = _findAncestorRenderObjectElement();
_ancestorRenderObjectElement?.insertChildRenderObject(renderObject, newSlot);
ParentDataElement parentDataElement = _findAncestorParentDataElement();
if (parentDataElement != null)
updateParentData(parentDataElement.widget);
}
void detachRenderObject() {

View File

@@ -284,4 +284,55 @@ void main() {
checkTree(tester, <TestParentData>[]);
});
});
test('ParentDataWidget interacts with global keys', () {
testWidgets((WidgetTester tester) {
GlobalKey key = new GlobalKey();
tester.pumpWidget(
new Stack(<Widget>[
new Positioned(
top: 10.0,
left: 10.0,
child: new DecoratedBox(key: key, decoration: kBoxDecorationA)
)
])
);
checkTree(tester, <TestParentData>[
new TestParentData(top: 10.0, left: 10.0),
]);
tester.pumpWidget(
new Stack(<Widget>[
new Positioned(
top: 10.0,
left: 10.0,
child: new DecoratedBox(
decoration: kBoxDecorationB,
child: new DecoratedBox(key: key, decoration: kBoxDecorationA)
)
)
])
);
checkTree(tester, <TestParentData>[
new TestParentData(top: 10.0, left: 10.0),
]);
tester.pumpWidget(
new Stack(<Widget>[
new Positioned(
top: 10.0,
left: 10.0,
child: new DecoratedBox(key: key, decoration: kBoxDecorationA)
)
])
);
checkTree(tester, <TestParentData>[
new TestParentData(top: 10.0, left: 10.0),
]);
});
});
}