Skip to content

Commit a8346d1

Browse files
committed
Guard FocusNode.rect/offset/size against an unlaid-out RenderBox
FocusNode.rect and offset read the attached render object's semanticBounds, which for a RenderBox reads its size. If the render object is attached but has not completed layout yet (for example a just-mounted Overlay that takes focus before the layout phase reaches it), this throws 'RenderBox was not laid out', which crashes the app even in release builds. Return a zero rect/offset (and therefore zero size) while the box has no size. The value is corrected on the next frame once layout runs and the focus node is measured again. Fixes flutter#191508.
1 parent d79b030 commit a8346d1

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

‎packages/flutter/lib/src/widgets/focus_manager.dart‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,11 @@ class FocusNode with DiagnosticableTreeMixin, ChangeNotifier {
851851
'Setting the context is typically done with the attach method.',
852852
);
853853
final RenderObject object = context!.findRenderObject()!;
854+
// A box that hasn't been laid out yet has no size to read, so report zero
855+
// until it does.
856+
if (object is RenderBox && !object.hasSize) {
857+
return Offset.zero;
858+
}
854859
return MatrixUtils.transformPoint(object.getTransformTo(null), object.semanticBounds.topLeft);
855860
}
856861

@@ -866,6 +871,11 @@ class FocusNode with DiagnosticableTreeMixin, ChangeNotifier {
866871
'Setting the context is typically done with the attach method.',
867872
);
868873
final RenderObject object = context!.findRenderObject()!;
874+
// A box that hasn't been laid out yet has no size to read, so report zero
875+
// until it does.
876+
if (object is RenderBox && !object.hasSize) {
877+
return Rect.zero;
878+
}
869879
final Offset topLeft = MatrixUtils.transformPoint(
870880
object.getTransformTo(null),
871881
object.semanticBounds.topLeft,

‎packages/flutter/test/widgets/focus_manager_test.dart‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,27 @@ void main() {
112112
expect(focusNode2.offset, equals(const Offset(443.0, 194.5)));
113113
});
114114

115+
test('rect, offset, and size return zero when the RenderObject is not laid out', () {
116+
// Regression test for https://github.com/flutter/flutter/issues/191508.
117+
// Reading the bounds of a focus node whose box isn't laid out yet used to
118+
// throw "RenderBox was not laid out"; it should report zero instead.
119+
final owner = PipelineOwner();
120+
addTearDown(() => owner.rootNode = null);
121+
final RenderBox box = RenderConstrainedBox(
122+
additionalConstraints: const BoxConstraints.tightFor(width: 10, height: 10),
123+
);
124+
owner.rootNode = box;
125+
expect(box.hasSize, isFalse);
126+
127+
final node = FocusNode();
128+
addTearDown(node.dispose);
129+
node.attach(_UnlaidOutBuildContext(box));
130+
131+
expect(node.rect, Rect.zero);
132+
expect(node.offset, Offset.zero);
133+
expect(node.size, Size.zero);
134+
});
135+
115136
testWidgets('descendantsAreFocusable disables focus for descendants.', (
116137
WidgetTester tester,
117138
) async {
@@ -2436,3 +2457,15 @@ class _LoggingTestFocusNode extends FocusNode {
24362457
throw StateError("Shouldn't call toStringDeep here");
24372458
}
24382459
}
2460+
2461+
class _UnlaidOutBuildContext implements BuildContext {
2462+
_UnlaidOutBuildContext(this._renderObject);
2463+
2464+
final RenderObject _renderObject;
2465+
2466+
@override
2467+
RenderObject findRenderObject() => _renderObject;
2468+
2469+
@override
2470+
dynamic noSuchMethod(Invocation invocation) => throw UnimplementedError();
2471+
}

0 commit comments

Comments
 (0)