From 35464f447bcb20000451469af23038ffe954ecd5 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Thu, 12 Nov 2015 16:51:34 -0800 Subject: [PATCH] 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 --- .../flutter/lib/src/widgets/framework.dart | 6 +-- .../unit/test/widget/parent_data_test.dart | 51 +++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/widgets/framework.dart b/packages/flutter/lib/src/widgets/framework.dart index 8d278b6e6e..070398f93d 100644 --- a/packages/flutter/lib/src/widgets/framework.dart +++ b/packages/flutter/lib/src/widgets/framework.dart @@ -1275,9 +1275,6 @@ abstract class RenderObjectElement 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 extends Buildab _slot = newSlot; _ancestorRenderObjectElement = _findAncestorRenderObjectElement(); _ancestorRenderObjectElement?.insertChildRenderObject(renderObject, newSlot); + ParentDataElement parentDataElement = _findAncestorParentDataElement(); + if (parentDataElement != null) + updateParentData(parentDataElement.widget); } void detachRenderObject() { diff --git a/packages/unit/test/widget/parent_data_test.dart b/packages/unit/test/widget/parent_data_test.dart index bc5eb2e922..59af8bfe87 100644 --- a/packages/unit/test/widget/parent_data_test.dart +++ b/packages/unit/test/widget/parent_data_test.dart @@ -284,4 +284,55 @@ void main() { checkTree(tester, []); }); }); + + test('ParentDataWidget interacts with global keys', () { + testWidgets((WidgetTester tester) { + GlobalKey key = new GlobalKey(); + + tester.pumpWidget( + new Stack([ + new Positioned( + top: 10.0, + left: 10.0, + child: new DecoratedBox(key: key, decoration: kBoxDecorationA) + ) + ]) + ); + + checkTree(tester, [ + new TestParentData(top: 10.0, left: 10.0), + ]); + + tester.pumpWidget( + new Stack([ + new Positioned( + top: 10.0, + left: 10.0, + child: new DecoratedBox( + decoration: kBoxDecorationB, + child: new DecoratedBox(key: key, decoration: kBoxDecorationA) + ) + ) + ]) + ); + + checkTree(tester, [ + new TestParentData(top: 10.0, left: 10.0), + ]); + + tester.pumpWidget( + new Stack([ + new Positioned( + top: 10.0, + left: 10.0, + child: new DecoratedBox(key: key, decoration: kBoxDecorationA) + ) + ]) + ); + + checkTree(tester, [ + new TestParentData(top: 10.0, left: 10.0), + ]); + }); + }); }