Skip to content

Commit 8e0c467

Browse files
square-tombstudioclaude
committed
fix(mobile): let a covered page follow its own keyboard closing
When a sheet takes focus over a page whose composer had the keyboard up, the page's keyboard closes behind the scrim. The covered page kept the open-keyboard inset, leaving an empty band and a one-frame jump when the sheet closed. While covered, follow the keyboard when it shrinks and keep ignoring it when it grows, so a keyboard raised by the covering route still causes no rebuilds below it. Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Tom Brow <[email protected]>
1 parent 7d728b0 commit 8e0c467

2 files changed

Lines changed: 66 additions & 5 deletions

File tree

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import 'package:flutter_hooks/flutter_hooks.dart';
99
/// reads `MediaQuery.of`, `paddingOf` or `viewInsetsOf` (directly or through
1010
/// a [Scaffold]) rebuilds and relays out its whole content for keyboard
1111
/// motion it cannot show. While the route is not current, [child] keeps the
12-
/// view insets and padding it last had as the current route; every other
13-
/// [MediaQueryData] field passes through. When the route becomes current
14-
/// again the live values return.
12+
/// view insets and padding it last had as the current route, except that it
13+
/// still follows the keyboard down: when a covering route takes focus and the
14+
/// page's own keyboard closes, the page lowers behind the scrim instead of
15+
/// jumping when it is uncovered. Every other [MediaQueryData] field passes
16+
/// through. When the route becomes current again the live values return.
1517
class CoveredRouteKeyboardInsets extends HookWidget {
1618
const CoveredRouteKeyboardInsets({super.key, required this.child});
1719

@@ -21,9 +23,13 @@ class CoveredRouteKeyboardInsets extends HookWidget {
2123
Widget build(BuildContext context) {
2224
final data = MediaQuery.of(context);
2325
final current = ModalRoute.isCurrentOf(context) ?? true;
24-
// The insets and padding from the last build as the current route.
26+
// The insets and padding from the last build as the current route, or
27+
// the smallest keyboard seen since it was covered.
2528
final held = useRef<(EdgeInsets, EdgeInsets)?>(null);
26-
if (current || held.value == null) {
29+
final heldValue = held.value;
30+
if (current ||
31+
heldValue == null ||
32+
data.viewInsets.bottom <= heldValue.$1.bottom) {
2733
held.value = (data.viewInsets, data.padding);
2834
}
2935
final (viewInsets, padding) = held.value!;

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,61 @@ void main() {
7474
expect(seen.last, const EdgeInsets.only(bottom: 300));
7575
});
7676

77+
testWidgets('a covered page follows its own keyboard closing', (
78+
tester,
79+
) async {
80+
final builds = [0];
81+
final seen = <EdgeInsets>[];
82+
final navigator = GlobalKey<NavigatorState>();
83+
var insets = const EdgeInsets.only(bottom: 300);
84+
late StateSetter setOuter;
85+
final app = MaterialApp(
86+
navigatorKey: navigator,
87+
home: CoveredRouteKeyboardInsets(child: _Probe(builds, seen)),
88+
);
89+
90+
await tester.pumpWidget(
91+
StatefulBuilder(
92+
builder: (context, setState) {
93+
setOuter = setState;
94+
return MediaQuery(
95+
data: MediaQueryData(
96+
size: const Size(400, 800),
97+
viewInsets: insets,
98+
padding: EdgeInsets.only(bottom: insets.bottom > 0 ? 0 : 34),
99+
),
100+
child: app,
101+
);
102+
},
103+
),
104+
);
105+
expect(seen.last, const EdgeInsets.only(bottom: 300));
106+
107+
// A sheet takes focus, so the page's keyboard closes behind it.
108+
navigator.currentState!.push(
109+
PageRouteBuilder<void>(
110+
opaque: false,
111+
transitionDuration: Duration.zero,
112+
pageBuilder: (_, _, _) => const SizedBox(),
113+
),
114+
);
115+
await tester.pumpAndSettle();
116+
for (final bottom in [200.0, 100.0, 0.0]) {
117+
setOuter(() => insets = EdgeInsets.only(bottom: bottom));
118+
await tester.pump();
119+
expect(seen.last, EdgeInsets.only(bottom: bottom));
120+
}
121+
122+
// A keyboard the covering route raises is still ignored.
123+
final coveredBuilds = builds[0];
124+
for (final bottom in [80.0, 160.0, 300.0]) {
125+
setOuter(() => insets = EdgeInsets.only(bottom: bottom));
126+
await tester.pump();
127+
}
128+
expect(builds[0], coveredBuilds);
129+
expect(seen.last, EdgeInsets.zero);
130+
});
131+
77132
testWidgets('a current page follows the keyboard', (tester) async {
78133
final builds = [0];
79134
final seen = <EdgeInsets>[];

0 commit comments

Comments
 (0)