Skip to content

Commit 368a6dc

Browse files
authored
Defer semantics geometry updates for blocked branches (flutter#192146)
When a dialog opens, its barrier hides the page behind it from the accessibility tree. The page can still move while hidden — dismissing the keyboard pushes the text field down. The framework did queue a geometry update for that move, then dropped it because the node was not in the semantics tree at that moment. When the dialog closed, nothing remembered the move, so the screen reader kept using the text field's old position — or lost the field entirely. This keeps those updates in a deferred set and retries them on each flush instead of dropping them. As soon as the branch rejoins the semantics tree, they run through the normal update path and every moved node gets its real position back. When nothing is blocked the set stays empty, so the cost is effectively zero. Fixes flutter#186178 ### Video Evidence https://github.com/user-attachments/assets/2b51ce02-8e79-445a-94aa-f6269f21729f ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent f4dbc3f commit 368a6dc

2 files changed

Lines changed: 228 additions & 6 deletions

File tree

‎packages/flutter/lib/src/rendering/object.dart‎

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,9 @@ base class PipelineOwner with DiagnosticableTreeMixin {
14061406
} else if (_semanticsOwner != null) {
14071407
_semanticsOwner?.dispose();
14081408
_semanticsOwner = null;
1409+
// Deferred updates would otherwise retain their render objects until
1410+
// this owner is disposed; re-enabling semantics rebuilds from scratch.
1411+
_deferredNodesNeedingSemanticsGeometryUpdate.clear();
14091412
onSemanticsOwnerDisposed?.call();
14101413
}
14111414
}
@@ -1435,6 +1438,15 @@ base class PipelineOwner with DiagnosticableTreeMixin {
14351438
/// this set only tracks the dirty nodes that need their semantics geometry updated directly.
14361439
final Set<RenderObject> _nodesNeedingSemanticsGeometryUpdate = <RenderObject>{};
14371440

1441+
/// Nodes whose geometry update was skipped by [flushSemantics] because they
1442+
/// were not part of the semantics tree at the time, e.g. their branch was
1443+
/// blocked by [BlockSemantics].
1444+
///
1445+
/// A blocked node can still move, so the update is retried on every flush
1446+
/// instead of dropped; otherwise the node would rejoin the tree with stale
1447+
/// geometry.
1448+
final Set<RenderObject> _deferredNodesNeedingSemanticsGeometryUpdate = <RenderObject>{};
1449+
14381450
/// Update the semantics for render objects marked as needing a semantics
14391451
/// update.
14401452
///
@@ -1506,12 +1518,30 @@ base class PipelineOwner with DiagnosticableTreeMixin {
15061518
// It is possible the updateChildren above caused some nodes to be added
15071519
// to _nodesNeedingSemanticsGeometryUpdate. Therefore, we need to
15081520
// process them here.
1509-
final List<RenderObject> nodesToProcessGeometry = _nodesNeedingSemanticsGeometryUpdate
1510-
.where(
1511-
(RenderObject object) =>
1512-
!object._needsLayout && object.owner == this && !object._semantics.parentDataDirty,
1513-
)
1514-
.toList();
1521+
//
1522+
// Deferred geometry updates are retried as well; if their branch
1523+
// rejoined the tree in the updateChildren phase above, they can now be
1524+
// applied.
1525+
if (_deferredNodesNeedingSemanticsGeometryUpdate.isNotEmpty) {
1526+
_nodesNeedingSemanticsGeometryUpdate.addAll(_deferredNodesNeedingSemanticsGeometryUpdate);
1527+
_deferredNodesNeedingSemanticsGeometryUpdate.clear();
1528+
}
1529+
final nodesToProcessGeometry = <RenderObject>[];
1530+
for (final RenderObject object in _nodesNeedingSemanticsGeometryUpdate) {
1531+
if (object._needsLayout || object.owner != this) {
1532+
// A node that still needs layout marks itself again after its next
1533+
// layout; a node that left this owner no longer needs the update.
1534+
continue;
1535+
}
1536+
if (object._semantics.parentDataDirty) {
1537+
// Not part of the semantics tree right now (e.g. blocked by
1538+
// BlockSemantics), but the node may still move. Defer the update
1539+
// until the branch rejoins instead of dropping it.
1540+
_deferredNodesNeedingSemanticsGeometryUpdate.add(object);
1541+
continue;
1542+
}
1543+
nodesToProcessGeometry.add(object);
1544+
}
15151545
_nodesNeedingSemanticsGeometryUpdate.clear();
15161546

15171547
// For every node in this list, needs to clear geometry immediate _RenderObjectSemantics
@@ -1818,6 +1848,7 @@ base class PipelineOwner with DiagnosticableTreeMixin {
18181848
_nodesNeedingCompositingBitsUpdate.clear();
18191849
_nodesNeedingPaint.clear();
18201850
_nodesNeedingSemanticsUpdate.clear();
1851+
_deferredNodesNeedingSemanticsGeometryUpdate.clear();
18211852
}
18221853
}
18231854

@@ -5889,6 +5920,10 @@ class _RenderObjectSemantics extends _SemanticsFragment with DiagnosticableTreeM
58895920
// Frame 2: B is marked dirty again and decide C should be included
58905921
// in the semantics tree. We will run into this situation where C's geometry
58915922
// is dirty but it is not in the _nodesNeedingSemanticsGeometryUpdate.
5923+
//
5924+
// A node that moved while its branch was blocked has stale, not
5925+
// dirty, geometry, so this check misses it. That case is covered by
5926+
// PipelineOwner._deferredNodesNeedingSemanticsGeometryUpdate.
58925927
if (childSemantics.geometryDirty) {
58935928
renderObject.owner!._nodesNeedingSemanticsGeometryUpdate.add(childSemantics.renderObject);
58945929
}
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/rendering.dart';
6+
import 'package:flutter/widgets.dart';
7+
import 'package:flutter_test/flutter_test.dart';
8+
9+
void main() {
10+
const targetKey = Key('target');
11+
12+
// A page with a semantics boundary, a text with an inline widget span, a
13+
// spacer that controls where the target sits, and a target that forms its
14+
// own semantics node. An optional BlockSemantics models a modal barrier
15+
// covering the page.
16+
Widget buildTest({required double spacerHeight, required bool blocked, required String text}) {
17+
return Directionality(
18+
textDirection: TextDirection.ltr,
19+
child: Stack(
20+
textDirection: TextDirection.ltr,
21+
children: <Widget>[
22+
Semantics(
23+
container: true,
24+
label: 'page',
25+
child: Column(
26+
crossAxisAlignment: CrossAxisAlignment.start,
27+
children: <Widget>[
28+
RichText(
29+
text: TextSpan(
30+
text: text,
31+
style: const TextStyle(fontSize: 10),
32+
children: const <InlineSpan>[WidgetSpan(child: SizedBox(width: 5, height: 5))],
33+
),
34+
),
35+
SizedBox(height: spacerHeight),
36+
Semantics(
37+
container: true,
38+
label: 'target',
39+
child: const SizedBox(key: targetKey, width: 100, height: 20),
40+
),
41+
],
42+
),
43+
),
44+
if (blocked)
45+
BlockSemantics(
46+
child: Semantics(container: true, label: 'barrier', child: const SizedBox.expand()),
47+
),
48+
],
49+
),
50+
);
51+
}
52+
53+
// The global rect of `node` in logical pixels. Semantics geometry is in
54+
// physical pixels, so the device pixel ratio is divided back out.
55+
Rect globalSemanticsRect(WidgetTester tester, SemanticsNode node) {
56+
Matrix4 transform = node.transform ?? Matrix4.identity();
57+
for (SemanticsNode? parent = node.parent; parent != null; parent = parent.parent) {
58+
if (parent.transform != null) {
59+
transform = parent.transform!.multiplied(transform);
60+
}
61+
}
62+
final Rect rect = MatrixUtils.transformRect(transform, node.rect);
63+
final double scale = tester.view.devicePixelRatio;
64+
return Rect.fromLTRB(
65+
rect.left / scale,
66+
rect.top / scale,
67+
rect.right / scale,
68+
rect.bottom / scale,
69+
);
70+
}
71+
72+
testWidgets('Regression test: semantics geometry is updated for a node that moved '
73+
'while its branch was blocked from the semantics tree', (WidgetTester tester) async {
74+
// Regression test for https://github.com/flutter/flutter/issues/186178.
75+
await tester.pumpWidget(buildTest(spacerHeight: 100.0, blocked: false, text: 'a'));
76+
expect(
77+
globalSemanticsRect(tester, tester.getSemantics(find.byKey(targetKey))),
78+
tester.getRect(find.byKey(targetKey)),
79+
);
80+
81+
// A modal barrier blocks the page, e.g. a dialog opens.
82+
await tester.pumpWidget(buildTest(spacerHeight: 100.0, blocked: true, text: 'a'));
83+
84+
// While the page is blocked its layout changes, e.g. the keyboard is
85+
// dismissed: the text re-layout dirties the parent data throughout the
86+
// blocked branch, and the spacer change moves the target without
87+
// resizing it.
88+
await tester.pumpWidget(buildTest(spacerHeight: 300.0, blocked: true, text: 'bb'));
89+
90+
// The barrier goes away, e.g. the dialog is closed.
91+
await tester.pumpWidget(buildTest(spacerHeight: 300.0, blocked: false, text: 'bb'));
92+
93+
final SemanticsNode target = tester.getSemantics(find.byKey(targetKey));
94+
expect(target.isInvisible, isFalse);
95+
expect(target.flagsCollection.isHidden, isFalse);
96+
expect(globalSemanticsRect(tester, target), tester.getRect(find.byKey(targetKey)));
97+
});
98+
99+
testWidgets('Regression test: semantics geometry is updated after the keyboard is dismissed '
100+
'while a modal route is up', (WidgetTester tester) async {
101+
// Regression test for https://github.com/flutter/flutter/issues/186178.
102+
final navigatorKey = GlobalKey<NavigatorState>();
103+
104+
// The keyboard is up.
105+
tester.view.viewInsets = const FakeViewPadding(bottom: 300.0);
106+
addTearDown(tester.view.reset);
107+
108+
await tester.pumpWidget(
109+
WidgetsApp(
110+
color: const Color(0xFF000000),
111+
navigatorKey: navigatorKey,
112+
onGenerateRoute: (RouteSettings settings) => PageRouteBuilder<void>(
113+
settings: settings,
114+
pageBuilder:
115+
(
116+
BuildContext context,
117+
Animation<double> animation,
118+
Animation<double> secondaryAnimation,
119+
) {
120+
return Column(
121+
children: <Widget>[
122+
const Expanded(child: SizedBox.expand()),
123+
Semantics(
124+
container: true,
125+
label: 'target',
126+
child: const SizedBox(key: targetKey, width: 100.0, height: 20.0),
127+
),
128+
// Stands in for the space taken by the keyboard.
129+
SizedBox(height: MediaQuery.viewInsetsOf(context).bottom),
130+
],
131+
);
132+
},
133+
),
134+
),
135+
);
136+
137+
// A modal route with a barrier opens, e.g. a dialog.
138+
navigatorKey.currentState!.push(
139+
PageRouteBuilder<void>(
140+
opaque: false,
141+
barrierColor: const Color(0x80000000),
142+
pageBuilder:
143+
(
144+
BuildContext context,
145+
Animation<double> animation,
146+
Animation<double> secondaryAnimation,
147+
) => Semantics(
148+
container: true,
149+
label: 'dialog',
150+
child: const SizedBox(width: 100.0, height: 100.0),
151+
),
152+
),
153+
);
154+
await tester.pump();
155+
156+
// The keyboard is dismissed while the modal route is up.
157+
tester.view.viewInsets = FakeViewPadding.zero;
158+
await tester.pumpAndSettle();
159+
160+
// The modal route is closed.
161+
navigatorKey.currentState!.pop();
162+
await tester.pumpAndSettle();
163+
164+
final SemanticsNode target = tester.getSemantics(find.byKey(targetKey));
165+
expect(globalSemanticsRect(tester, target), tester.getRect(find.byKey(targetKey)));
166+
});
167+
168+
testWidgets('blocked branch with pending geometry update can be removed', (
169+
WidgetTester tester,
170+
) async {
171+
await tester.pumpWidget(buildTest(spacerHeight: 100.0, blocked: false, text: 'a'));
172+
await tester.pumpWidget(buildTest(spacerHeight: 100.0, blocked: true, text: 'a'));
173+
await tester.pumpWidget(buildTest(spacerHeight: 300.0, blocked: true, text: 'bb'));
174+
175+
// The whole page, including the subtree with the pending geometry update,
176+
// goes away while it is still blocked.
177+
await tester.pumpWidget(
178+
Directionality(
179+
textDirection: TextDirection.ltr,
180+
child: Semantics(container: true, label: 'other', child: const SizedBox.expand()),
181+
),
182+
);
183+
184+
expect(tester.takeException(), isNull);
185+
expect(tester.getSemantics(find.bySemanticsLabel('other')), isNotNull);
186+
});
187+
}

0 commit comments

Comments
 (0)