Skip to content

Commit 2a92ddd

Browse files
square-tombclaude
andcommitted
fix(mobile): keep a page still when it is uncovered with the keyboard up
When a covering route's keyboard was still up as the route popped, the page below jumped from its held insets to the live keyboard height and then rode back down as the keyboard closed. After the route becomes current again, the page now keeps its held insets until the keyboard comes down to the held value, or until focus is inside the route (the page raised its own keyboard). Adds tests for the uncover case, the follow-down release and the refocus case. Co-Authored-By: Claude Sonnet <[email protected]> Signed-off-by: Tom Brow <[email protected]>
1 parent 3155ef8 commit 2a92ddd

2 files changed

Lines changed: 189 additions & 8 deletions

File tree

‎mobile/lib/shared/widgets/covered_route_keyboard_insets.dart‎

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@ import 'package:flutter_hooks/flutter_hooks.dart';
1414
/// page's own keyboard closes, the page lowers behind the scrim instead of
1515
/// jumping when it is uncovered. A size change (rotation) drops the held
1616
/// values, so safe-area padding stays right for the new orientation. Every
17-
/// other [MediaQueryData] field passes through. When the route becomes
18-
/// current again the live values return.
17+
/// other [MediaQueryData] field passes through.
18+
///
19+
/// When the route becomes current again with the keyboard still above the
20+
/// held value (the covering route's keyboard has not finished closing), the
21+
/// page keeps the held values instead of jumping up to the live keyboard and
22+
/// riding it back down. It releases to the live values once the keyboard
23+
/// comes down to the held value, or as soon as focus is inside this route
24+
/// (the page raised its own keyboard and has to follow it).
1925
class CoveredRouteKeyboardInsets extends HookWidget {
2026
const CoveredRouteKeyboardInsets({super.key, required this.child});
2127

@@ -25,23 +31,62 @@ class CoveredRouteKeyboardInsets extends HookWidget {
2531
Widget build(BuildContext context) {
2632
final data = MediaQuery.of(context);
2733
final current = ModalRoute.isCurrentOf(context) ?? true;
34+
// Focus moving into the route must release the held values even when the
35+
// keyboard height is not changing at that moment.
36+
useListenable(FocusManager.instance);
37+
final focusInside = _hasFocusInside(FocusScope.of(context));
2838
// The insets and padding from the last build as the current route, or
2939
// the smallest keyboard seen since it was covered at the current size.
3040
final held = useRef<(EdgeInsets, EdgeInsets, Size)?>(null);
41+
// Whether the last build was as a covered route.
42+
final covered = useRef(false);
43+
// Whether the route is current again but still shows the held values.
44+
final settling = useRef(false);
3145
final heldValue = held.value;
32-
if (current ||
33-
heldValue == null ||
34-
data.size != heldValue.$3 ||
35-
data.viewInsets.bottom <= heldValue.$1.bottom) {
36-
held.value = (data.viewInsets, data.padding, data.size);
46+
final sizeChanged = heldValue != null && data.size != heldValue.$3;
47+
if (!current) {
48+
covered.value = true;
49+
settling.value = false;
50+
if (heldValue == null ||
51+
sizeChanged ||
52+
data.viewInsets.bottom <= heldValue.$1.bottom) {
53+
held.value = (data.viewInsets, data.padding, data.size);
54+
}
55+
} else {
56+
if (covered.value &&
57+
heldValue != null &&
58+
!sizeChanged &&
59+
!focusInside &&
60+
data.viewInsets.bottom > heldValue.$1.bottom) {
61+
settling.value = true;
62+
}
63+
covered.value = false;
64+
if (settling.value &&
65+
(sizeChanged ||
66+
focusInside ||
67+
data.viewInsets.bottom <= heldValue!.$1.bottom)) {
68+
settling.value = false;
69+
}
70+
if (!settling.value) {
71+
held.value = (data.viewInsets, data.padding, data.size);
72+
}
3773
}
3874
final (viewInsets, padding, _) = held.value!;
3975
// Always the same widget shape, so covering never remounts the page.
4076
return MediaQuery(
41-
data: current
77+
data: current && !settling.value
4278
? data
4379
: data.copyWith(viewInsets: viewInsets, padding: padding),
4480
child: child,
4581
);
4682
}
83+
84+
/// Whether primary focus is on a descendant of [scope], not [scope] itself
85+
/// (which holds focus when nothing inside the route has it).
86+
static bool _hasFocusInside(FocusScopeNode scope) {
87+
final primary = FocusManager.instance.primaryFocus;
88+
return primary != null &&
89+
primary != scope &&
90+
primary.ancestors.contains(scope);
91+
}
4792
}

‎mobile/test/shared/widgets/covered_route_keyboard_insets_test.dart‎

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,145 @@ void main() {
6969
expect(builds[0], coveredBuilds);
7070
expect(seen.last, EdgeInsets.zero);
7171

72+
// Uncovered while the covering route's keyboard is still up: the page
73+
// stays where it was instead of jumping up, then takes live values once
74+
// the keyboard is gone.
7275
navigator.currentState!.pop();
7376
await tester.pumpAndSettle();
77+
expect(seen.last, EdgeInsets.zero);
78+
for (final bottom in [200.0, 100.0]) {
79+
setOuter(() => insets = EdgeInsets.only(bottom: bottom));
80+
await tester.pump();
81+
expect(seen.last, EdgeInsets.zero);
82+
}
83+
setOuter(() => insets = EdgeInsets.zero);
84+
await tester.pump();
85+
expect(seen.last, EdgeInsets.zero);
86+
setOuter(() => insets = const EdgeInsets.only(bottom: 120));
87+
await tester.pump();
88+
expect(seen.last, const EdgeInsets.only(bottom: 120));
89+
});
90+
91+
testWidgets('an uncovered page never rises above the live keyboard and '
92+
'follows it once it reaches the held value', (tester) async {
93+
final builds = [0];
94+
final seen = <EdgeInsets>[];
95+
final navigator = GlobalKey<NavigatorState>();
96+
var insets = const EdgeInsets.only(bottom: 300);
97+
late StateSetter setOuter;
98+
final app = MaterialApp(
99+
navigatorKey: navigator,
100+
home: CoveredRouteKeyboardInsets(child: _Probe(builds, seen)),
101+
);
102+
103+
await tester.pumpWidget(
104+
StatefulBuilder(
105+
builder: (context, setState) {
106+
setOuter = setState;
107+
return MediaQuery(
108+
data: MediaQueryData(
109+
size: const Size(400, 800),
110+
viewInsets: insets,
111+
),
112+
child: app,
113+
);
114+
},
115+
),
116+
);
117+
118+
navigator.currentState!.push(
119+
PageRouteBuilder<void>(
120+
opaque: false,
121+
transitionDuration: Duration.zero,
122+
pageBuilder: (_, _, _) => const SizedBox(),
123+
),
124+
);
125+
await tester.pumpAndSettle();
126+
// The page's keyboard closes to 100 behind the covering route, whose own
127+
// field then raises the keyboard back to 300.
128+
for (final bottom in [200.0, 100.0, 200.0, 300.0]) {
129+
setOuter(() => insets = EdgeInsets.only(bottom: bottom));
130+
await tester.pump();
131+
}
132+
expect(seen.last, const EdgeInsets.only(bottom: 100));
133+
134+
navigator.currentState!.pop();
135+
await tester.pumpAndSettle();
136+
expect(seen.last, const EdgeInsets.only(bottom: 100));
137+
for (final bottom in [250.0, 150.0, 100.0]) {
138+
setOuter(() => insets = EdgeInsets.only(bottom: bottom));
139+
await tester.pump();
140+
expect(seen.last, const EdgeInsets.only(bottom: 100));
141+
}
142+
setOuter(() => insets = const EdgeInsets.only(bottom: 40));
143+
await tester.pump();
144+
expect(seen.last, const EdgeInsets.only(bottom: 40));
145+
});
146+
147+
testWidgets('an uncovered page follows the keyboard its own field raises', (
148+
tester,
149+
) async {
150+
final builds = [0];
151+
final seen = <EdgeInsets>[];
152+
final navigator = GlobalKey<NavigatorState>();
153+
final focusNode = FocusNode();
154+
addTearDown(focusNode.dispose);
155+
var insets = EdgeInsets.zero;
156+
late StateSetter setOuter;
157+
final app = MaterialApp(
158+
navigatorKey: navigator,
159+
home: CoveredRouteKeyboardInsets(
160+
child: Scaffold(
161+
resizeToAvoidBottomInset: false,
162+
body: Column(
163+
children: [
164+
Expanded(child: _Probe(builds, seen)),
165+
TextField(focusNode: focusNode),
166+
],
167+
),
168+
),
169+
),
170+
);
171+
172+
await tester.pumpWidget(
173+
StatefulBuilder(
174+
builder: (context, setState) {
175+
setOuter = setState;
176+
return MediaQuery(
177+
data: MediaQueryData(
178+
size: const Size(400, 800),
179+
viewInsets: insets,
180+
),
181+
child: app,
182+
);
183+
},
184+
),
185+
);
186+
187+
navigator.currentState!.push(
188+
PageRouteBuilder<void>(
189+
opaque: false,
190+
transitionDuration: Duration.zero,
191+
pageBuilder: (_, _, _) => const SizedBox(),
192+
),
193+
);
194+
await tester.pumpAndSettle();
195+
setOuter(() => insets = const EdgeInsets.only(bottom: 300));
196+
await tester.pump();
197+
navigator.currentState!.pop();
198+
await tester.pumpAndSettle();
199+
expect(seen.last, EdgeInsets.zero);
200+
201+
// The page's own field takes focus while the keyboard is still up, so the
202+
// page has to follow the keyboard rather than hold below it.
203+
focusNode.requestFocus();
204+
await tester.pump();
205+
await tester.pump();
206+
expect(focusNode.hasPrimaryFocus, isTrue);
74207
expect(seen.last, const EdgeInsets.only(bottom: 300));
208+
setOuter(() => insets = const EdgeInsets.only(bottom: 280));
209+
await tester.pump();
210+
expect(seen.last, const EdgeInsets.only(bottom: 280));
75211
});
76212

77213
testWidgets('a covered page follows its own keyboard closing', (

0 commit comments

Comments
 (0)