Unverified Commit 1e7f3955 authored by xubaolin's avatar xubaolin Committed by GitHub

fix a RenderBox.size access exception (#74402)

parent b4f69eb7
......@@ -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),
......
......@@ -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.
......
......@@ -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<void> 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 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment