diff --git a/packages/flutter/lib/src/rendering/box.dart b/packages/flutter/lib/src/rendering/box.dart index 7718974e46..96d7070d33 100644 --- a/packages/flutter/lib/src/rendering/box.dart +++ b/packages/flutter/lib/src/rendering/box.dart @@ -1942,7 +1942,8 @@ abstract class RenderBox extends RenderObject { final Size? _size = this._size; if (_size is _DebugSize) { assert(_size._owner == this); - if (RenderObject.debugActiveLayout != null) { + if (RenderObject.debugActiveLayout != null && + !RenderObject.debugActiveLayout!.debugDoingThisLayoutWithCallback) { assert( debugDoingThisResize || debugDoingThisLayout || _computingThisDryLayout || (RenderObject.debugActiveLayout == parent && _size._canBeUsedByParent), diff --git a/packages/flutter/lib/src/rendering/object.dart b/packages/flutter/lib/src/rendering/object.dart index 1820459580..fc090611a5 100644 --- a/packages/flutter/lib/src/rendering/object.dart +++ b/packages/flutter/lib/src/rendering/object.dart @@ -1445,6 +1445,9 @@ abstract class RenderObject extends AbstractNode with DiagnosticableTreeMixin im bool _needsLayout = true; RenderObject? _relayoutBoundary; + + /// Whether [invokeLayoutCallback] for this render object is currently running. + bool get debugDoingThisLayoutWithCallback => _doingThisLayoutWithCallback; bool _doingThisLayoutWithCallback = false; /// The layout constraints most recently supplied by the parent. diff --git a/packages/flutter/test/widgets/layout_builder_test.dart b/packages/flutter/test/widgets/layout_builder_test.dart index 2e9706e636..545422e07c 100644 --- a/packages/flutter/test/widgets/layout_builder_test.dart +++ b/packages/flutter/test/widgets/layout_builder_test.dart @@ -663,6 +663,38 @@ void main() { expect(spy.performLayoutCount, 3); expect(spy.performResizeCount, 2); }); + + testWidgets('LayoutBuilder descendant widget can access [RenderBox.size] when rebuilding during layout', (WidgetTester tester) async { + Size? childSize; + int buildCount = 0; + + Future pumpTestWidget(Size size) async { + await tester.pumpWidget( + // Center is used to give the SizedBox the power to determine constraints for LayoutBuilder + Center( + child: SizedBox.fromSize( + size: size, + child: LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) { + buildCount++; + if (buildCount > 1) { + final _RenderLayoutSpy spy = tester.renderObject(find.byType(_LayoutSpy)); + childSize = spy.size; + } + return ColoredBox( + color: const Color(0xffffffff), + child: _LayoutSpy(), + ); + }), + ), + ), + ); + } + + await pumpTestWidget(const Size(10.0, 10.0)); + expect(childSize, isNull); + await pumpTestWidget(const Size(10.0, 10.0)); + expect(childSize, const Size(10.0, 10.0)); + }); } class _LayoutSpy extends LeafRenderObjectWidget {