Skip to content

Commit 63881f9

Browse files
authored
[material_ui] Paint Dialog.fullscreen background color behind the keyboard (flutter#13244)
Found while investigating flutter#179482 (design: https://flutter.dev/go/ios-26-keyboard-bleed). Companion to flutter#13242, which does the same for `Scaffold` persistent bottom sheets. `Dialog` builds `AnimatedPadding(viewInsets)` → `Material`, so when a text field inside a `Dialog.fullscreen` is focused, the "fullscreen" surface shrinks to the area above the keyboard and the `DialogRoute` barrier is what sits behind the keyboard. On iOS 26 the keyboard is translucent, so the dimmed route underneath shows through under the dialog instead of the dialog's color. Since `Dialog.fullscreen` also removes `viewInsets` from `child`, apps can't work around it from inside the dialog. For `Dialog.fullscreen`, the `AnimatedPadding` and `MediaQuery.removeViewInsets` now sit inside the `Material`, so the surface keeps covering the whole screen while `child` is laid out above the keyboard exactly as before. Regular `Dialog`s are unchanged. Part of flutter#179482. ## Before <img width="402" alt="before: the dimmed route behind the dialog shows through the keyboard" src="https://raw.githubusercontent.com/victorsanni/packages/assets-fullscreen-dialog-keyboard/fullscreen_dialog_before.png" /> ## After <img width="402" alt="after: the dialog's surface continues behind the keyboard" src="https://raw.githubusercontent.com/victorsanni/packages/assets-fullscreen-dialog-keyboard/fullscreen_dialog_after.png" /> ## Pre-Review Checklist [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent afe281a commit 63881f9

3 files changed

Lines changed: 94 additions & 30 deletions

File tree

‎packages/material_ui/lib/src/dialog.dart‎

Lines changed: 53 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ class Dialog extends StatelessWidget {
103103
/// Creates a fullscreen dialog.
104104
///
105105
/// Typically used in conjunction with [showDialog].
106+
///
107+
/// The dialog's [Material] covers the whole screen, including the area
108+
/// behind the software keyboard, while [child] is kept above the keyboard,
109+
/// so that a translucent keyboard shows the dialog's [backgroundColor]
110+
/// rather than what is behind the dialog.
106111
const Dialog.fullscreen({
107112
super.key,
108113
this.backgroundColor,
@@ -277,47 +282,65 @@ class Dialog extends StatelessWidget {
277282
final BoxConstraints boxConstraints =
278283
constraints ?? dialogTheme.constraints ?? const BoxConstraints(minWidth: 280.0);
279284

280-
Widget dialogChild;
285+
final Widget dialogChild;
281286

282287
if (_fullscreen) {
288+
// The Material keeps covering the whole screen, including the area
289+
// behind the keyboard, so that a translucent keyboard shows the dialog's
290+
// surface rather than the modal barrier behind it. Only the content is
291+
// kept above the keyboard.
283292
dialogChild = Material(
284293
color: backgroundColor ?? dialogTheme.backgroundColor ?? defaults.backgroundColor,
285-
child: child,
294+
child: child == null
295+
? null
296+
: _avoidViewInsets(context, padding: effectivePadding, child: child!),
286297
);
287298
} else {
288-
dialogChild = Align(
289-
alignment: alignment ?? dialogTheme.alignment ?? defaults.alignment!,
290-
child: ConstrainedBox(
291-
constraints: boxConstraints,
292-
child: Material(
293-
color: backgroundColor ?? dialogTheme.backgroundColor ?? defaults.backgroundColor,
294-
elevation: elevation ?? dialogTheme.elevation ?? defaults.elevation!,
295-
shadowColor: shadowColor ?? dialogTheme.shadowColor ?? defaults.shadowColor,
296-
surfaceTintColor:
297-
surfaceTintColor ?? dialogTheme.surfaceTintColor ?? defaults.surfaceTintColor,
298-
shape: shape ?? dialogTheme.shape ?? defaults.shape!,
299-
type: MaterialType.card,
300-
clipBehavior: clipBehavior ?? dialogTheme.clipBehavior ?? defaults.clipBehavior!,
301-
child: child,
299+
dialogChild = _avoidViewInsets(
300+
context,
301+
padding: effectivePadding,
302+
child: Align(
303+
alignment: alignment ?? dialogTheme.alignment ?? defaults.alignment!,
304+
child: ConstrainedBox(
305+
constraints: boxConstraints,
306+
child: Material(
307+
color: backgroundColor ?? dialogTheme.backgroundColor ?? defaults.backgroundColor,
308+
elevation: elevation ?? dialogTheme.elevation ?? defaults.elevation!,
309+
shadowColor: shadowColor ?? dialogTheme.shadowColor ?? defaults.shadowColor,
310+
surfaceTintColor:
311+
surfaceTintColor ?? dialogTheme.surfaceTintColor ?? defaults.surfaceTintColor,
312+
shape: shape ?? dialogTheme.shape ?? defaults.shape!,
313+
type: MaterialType.card,
314+
clipBehavior: clipBehavior ?? dialogTheme.clipBehavior ?? defaults.clipBehavior!,
315+
child: child,
316+
),
302317
),
303318
),
304319
);
305320
}
306321

307-
return Semantics(
308-
role: semanticsRole,
309-
child: AnimatedPadding(
310-
padding: effectivePadding,
311-
duration: insetAnimationDuration,
312-
curve: insetAnimationCurve,
313-
child: MediaQuery.removeViewInsets(
314-
removeLeft: true,
315-
removeTop: true,
316-
removeRight: true,
317-
removeBottom: true,
318-
context: context,
319-
child: dialogChild,
320-
),
322+
return Semantics(role: semanticsRole, child: dialogChild);
323+
}
324+
325+
// Keeps [child] out of the view insets, such as the software keyboard, which
326+
// the child then no longer needs to account for. [padding] is the view
327+
// insets plus the dialog's inset padding.
328+
Widget _avoidViewInsets(
329+
BuildContext context, {
330+
required EdgeInsets padding,
331+
required Widget child,
332+
}) {
333+
return AnimatedPadding(
334+
padding: padding,
335+
duration: insetAnimationDuration,
336+
curve: insetAnimationCurve,
337+
child: MediaQuery.removeViewInsets(
338+
removeLeft: true,
339+
removeTop: true,
340+
removeRight: true,
341+
removeBottom: true,
342+
context: context,
343+
child: child,
321344
),
322345
);
323346
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Keeps the `Material` of a `Dialog.fullscreen` covering the whole screen while the software keyboard is shown, so that a translucent keyboard shows the dialog's surface instead of the modal barrier behind it.
3+
version: patch

‎packages/material_ui/test/dialog_test.dart‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,6 +1665,44 @@ void main() {
16651665
);
16661666
});
16671667

1668+
testWidgets('Dialog.fullscreen Material extends behind the keyboard, child stays above it', (
1669+
WidgetTester tester,
1670+
) async {
1671+
late BuildContext childContext;
1672+
final Widget dialog = Dialog.fullscreen(
1673+
child: Builder(
1674+
builder: (BuildContext context) {
1675+
childContext = context;
1676+
return const Placeholder();
1677+
},
1678+
),
1679+
);
1680+
final Finder material = find.descendant(
1681+
of: find.byType(Dialog),
1682+
matching: find.byType(Material),
1683+
);
1684+
1685+
await tester.pumpWidget(
1686+
MediaQuery(
1687+
data: const MediaQueryData(viewInsets: EdgeInsets.only(bottom: 200.0)),
1688+
child: dialog,
1689+
),
1690+
);
1691+
await tester.pumpAndSettle();
1692+
// The child is kept above the keyboard and does not see the inset.
1693+
expect(tester.getRect(find.byType(Placeholder)), const Rect.fromLTRB(0.0, 0.0, 800.0, 400.0));
1694+
expect(MediaQuery.viewInsetsOf(childContext), EdgeInsets.zero);
1695+
// The dialog's surface keeps covering the whole screen, including the
1696+
// area behind the keyboard.
1697+
expect(tester.getRect(material), const Rect.fromLTRB(0.0, 0.0, 800.0, 600.0));
1698+
1699+
// Hiding the keyboard gives the child the whole screen back.
1700+
await tester.pumpWidget(MediaQuery(data: const MediaQueryData(), child: dialog));
1701+
await tester.pumpAndSettle();
1702+
expect(tester.getRect(find.byType(Placeholder)), const Rect.fromLTRB(0.0, 0.0, 800.0, 600.0));
1703+
expect(tester.getRect(material), const Rect.fromLTRB(0.0, 0.0, 800.0, 600.0));
1704+
});
1705+
16681706
// Regression test for https://github.com/flutter/flutter/issues/78229.
16691707
testWidgets('AlertDialog has correct semantics for content in iOS', (WidgetTester tester) async {
16701708
final semantics = SemanticsTester(tester);

0 commit comments

Comments
 (0)