Skip to content

Commit a355d5d

Browse files
authored
[material_ui] Paint Scaffold persistent bottom sheet background color behind the keyboard (flutter#13242)
Ports flutter#186365 to `material_ui` (design: https://flutter.dev/go/ios-26-keyboard-bleed). With a persistent bottom sheet (`Scaffold.bottomSheet` or `ScaffoldState.showBottomSheet`) and `resizeToAvoidBottomInset: true`, the sheet is lifted above the keyboard and the `Scaffold`'s own `Material` is what sits behind the keyboard. On iOS 26 the keyboard is translucent, so the scaffold color shows through under the sheet instead of the sheet's color. `_ScaffoldLayout` now extends the persistent sheet's slot by the part of the keyboard inset not already covered by the bottom bars, while still reporting the content-only height so FAB and `SnackBar` geometry are unchanged. `_StandardBottomSheet` pads its content inside the `BottomSheet`'s `Material` by that amount (growing the sheet's constraints to match), so the sheet's surface continues behind the keyboard. `resizeToAvoidBottomInset: false`, `showModalBottomSheet`, and `BottomSheet`'s public API are unchanged. Fixes the framework-owned part of flutter#179482. ## Before <img width="368" height="405" alt="before" src="https://github.com/user-attachments/assets/d60ec5b8-60e7-4ea5-881e-d1fe80fa2e4f" /> ## After <img width="368" height="405" alt="after" src="https://github.com/user-attachments/assets/0f8ae510-40bc-497f-b9a8-9545b5a42e43" /> ## 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 46c69b3 commit a355d5d

3 files changed

Lines changed: 426 additions & 9 deletions

File tree

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

Lines changed: 103 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,46 @@ class _BodyBoxConstraints extends BoxConstraints {
983983
Object.hash(super.hashCode, materialBannerHeight, bottomWidgetsHeight, appBarHeight);
984984
}
985985

986+
// Used to communicate the keyboard inset computed by _ScaffoldLayout to the
987+
// persistent bottom sheet, in the same way _BodyBoxConstraints communicates
988+
// layout information to the body.
989+
class _BottomSheetBoxConstraints extends BoxConstraints {
990+
const _BottomSheetBoxConstraints({super.maxWidth, super.maxHeight, required this.keyboardInset})
991+
: assert(keyboardInset >= 0);
992+
993+
// The distance between the bottom of the sheet's content, which avoids the
994+
// keyboard, and the bottom edge of the sheet's Material, which extends
995+
// behind it. Zero when the keyboard is closed or when
996+
// Scaffold.resizeToAvoidBottomInset is false.
997+
final double keyboardInset;
998+
999+
@override
1000+
bool operator ==(Object other) {
1001+
if (super != other) {
1002+
return false;
1003+
}
1004+
return other is _BottomSheetBoxConstraints && other.keyboardInset == keyboardInset;
1005+
}
1006+
1007+
@override
1008+
int get hashCode => Object.hash(super.hashCode, keyboardInset);
1009+
}
1010+
1011+
// Provides _BottomSheetBoxConstraints.keyboardInset to the _StandardBottomSheets
1012+
// in the Scaffold's bottom sheet slot, which are built before layout.
1013+
class _BottomSheetKeyboardInset extends InheritedWidget {
1014+
const _BottomSheetKeyboardInset({required this.inset, required super.child});
1015+
1016+
final double inset;
1017+
1018+
static double of(BuildContext context) {
1019+
return context.dependOnInheritedWidgetOfExactType<_BottomSheetKeyboardInset>()?.inset ?? 0.0;
1020+
}
1021+
1022+
@override
1023+
bool updateShouldNotify(_BottomSheetKeyboardInset oldWidget) => inset != oldWidget.inset;
1024+
}
1025+
9861026
// Used when Scaffold.extendBody is true to wrap the scaffold's body in a MediaQuery
9871027
// whose padding accounts for the height of the bottomNavigationBar and/or the
9881028
// persistentFooterButtons.
@@ -1135,6 +1175,15 @@ class _ScaffoldLayout extends MultiChildLayoutDelegate {
11351175
bottom - math.max(minInsets.bottom, bottomWidgetsHeight),
11361176
);
11371177

1178+
// When the keyboard lifts the bottom sheet's content above it, the sheet's
1179+
// Material keeps extending down to the edge otherwise available to the
1180+
// sheet (above any bottom bars), so that a translucent keyboard shows the
1181+
// sheet's color rather than the Scaffold's background.
1182+
final double bottomSheetKeyboardInset = math.max(
1183+
0.0,
1184+
bottom - bottomWidgetsHeight - contentBottom,
1185+
);
1186+
11381187
if (hasChild(_ScaffoldSlot.body)) {
11391188
double bodyMaxHeight = math.max(0.0, contentBottom - contentTop);
11401189

@@ -1189,14 +1238,23 @@ class _ScaffoldLayout extends MultiChildLayoutDelegate {
11891238
}
11901239

11911240
if (hasChild(_ScaffoldSlot.bottomSheet)) {
1192-
final bottomSheetConstraints = BoxConstraints(
1241+
final double bottomSheetBottom = contentBottom + bottomSheetKeyboardInset;
1242+
final bottomSheetConstraints = _BottomSheetBoxConstraints(
11931243
maxWidth: fullWidthConstraints.maxWidth,
1194-
maxHeight: math.max(0.0, contentBottom - contentTop),
1244+
maxHeight: math.max(0.0, bottomSheetBottom - contentTop),
1245+
keyboardInset: bottomSheetKeyboardInset,
11951246
);
1196-
bottomSheetSize = layoutChild(_ScaffoldSlot.bottomSheet, bottomSheetConstraints);
1247+
final Size sheetSize = layoutChild(_ScaffoldSlot.bottomSheet, bottomSheetConstraints);
11971248
positionChild(
11981249
_ScaffoldSlot.bottomSheet,
1199-
Offset((size.width - bottomSheetSize.width) / 2.0, contentBottom - bottomSheetSize.height),
1250+
Offset((size.width - sheetSize.width) / 2.0, bottomSheetBottom - sheetSize.height),
1251+
);
1252+
// The floating action button is positioned relative to the sheet's
1253+
// content, which ends at contentBottom, not to the Material behind the
1254+
// keyboard.
1255+
bottomSheetSize = Size(
1256+
sheetSize.width,
1257+
math.max(0.0, sheetSize.height - bottomSheetKeyboardInset),
12001258
);
12011259
}
12021260

@@ -1997,6 +2055,13 @@ class Scaffold extends StatefulWidget {
19972055
/// [showBottomSheet] and [showModalBottomSheet]. Typically it's a widget
19982056
/// that includes [Material].
19992057
///
2058+
/// When [resizeToAvoidBottomInset] is true and the software keyboard is
2059+
/// shown, the sheet's content is kept above the keyboard while the sheet's
2060+
/// [Material] continues behind it, so that a translucent keyboard shows the
2061+
/// sheet's surface rather than the scaffold's [backgroundColor]. That surface
2062+
/// is configured with [BottomSheetThemeData]; decorations painted by the
2063+
/// [bottomSheet] widget itself do not extend behind the keyboard.
2064+
///
20002065
/// See also:
20012066
///
20022067
/// * [showBottomSheet], which displays a bottom sheet as a route that can
@@ -2735,6 +2800,12 @@ class ScaffoldState extends State<Scaffold>
27352800
///
27362801
/// </callout-box>
27372802
///
2803+
/// When [Scaffold.resizeToAvoidBottomInset] is true and the software keyboard
2804+
/// is shown, the sheet's content is kept above the keyboard while the sheet's
2805+
/// [Material] continues behind it, so that a translucent keyboard shows the
2806+
/// sheet's surface rather than the scaffold's background. Use
2807+
/// [backgroundColor] or [BottomSheetThemeData] to configure that surface.
2808+
///
27382809
/// See also:
27392810
///
27402811
/// * [BottomSheet], which becomes the parent of the widget returned by the
@@ -3164,9 +3235,16 @@ class ScaffoldState extends State<Scaffold>
31643235
double? snackBarWidth;
31653236

31663237
if (_currentBottomSheet != null || _dismissedBottomSheets.isNotEmpty) {
3167-
final Widget stack = Stack(
3168-
alignment: Alignment.bottomCenter,
3169-
children: <Widget>[..._dismissedBottomSheets, ?_currentBottomSheet?._widget],
3238+
final Widget stack = LayoutBuilder(
3239+
builder: (BuildContext context, BoxConstraints constraints) {
3240+
return _BottomSheetKeyboardInset(
3241+
inset: (constraints as _BottomSheetBoxConstraints).keyboardInset,
3242+
child: Stack(
3243+
alignment: Alignment.bottomCenter,
3244+
children: <Widget>[..._dismissedBottomSheets, ?_currentBottomSheet?._widget],
3245+
),
3246+
);
3247+
},
31703248
);
31713249
_addIfNonNull(
31723250
children,
@@ -3505,6 +3583,14 @@ class _StandardBottomSheetState extends State<_StandardBottomSheet> {
35053583

35063584
@override
35073585
Widget build(BuildContext context) {
3586+
// Non-zero only while the keyboard lifts this sheet, see _ScaffoldLayout.
3587+
final double keyboardInset = _BottomSheetKeyboardInset.of(context);
3588+
// The constraints describe the content, so they grow by the part of the
3589+
// Material that extends behind the keyboard. This mirrors BottomSheet's own
3590+
// lookup; its defaults, used when both are null, don't limit the height.
3591+
final BoxConstraints? constraints =
3592+
widget.constraints ?? Theme.of(context).bottomSheetTheme.constraints;
3593+
35083594
return AnimatedBuilder(
35093595
animation: widget.animationController,
35103596
builder: (BuildContext context, Widget? child) {
@@ -3526,12 +3612,20 @@ class _StandardBottomSheetState extends State<_StandardBottomSheet> {
35263612
onDragStart: _handleDragStart,
35273613
onDragEnd: _handleDragEnd,
35283614
onClosing: widget.onClosing!,
3529-
builder: widget.builder,
3615+
// Extends the Material behind the keyboard. Always present, so the
3616+
// content keeps its state when the keyboard shows or hides.
3617+
builder: (BuildContext context) => Padding(
3618+
padding: EdgeInsets.only(bottom: keyboardInset),
3619+
child: widget.builder(context),
3620+
),
35303621
backgroundColor: widget.backgroundColor,
35313622
elevation: widget.elevation,
35323623
shape: widget.shape,
35333624
clipBehavior: widget.clipBehavior,
3534-
constraints: widget.constraints,
3625+
constraints: constraints?.copyWith(
3626+
minHeight: constraints.minHeight + keyboardInset,
3627+
maxHeight: constraints.maxHeight + keyboardInset,
3628+
),
35353629
),
35363630
),
35373631
),
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Extends the `Material` of a `Scaffold` persistent bottom sheet behind the software keyboard when `resizeToAvoidBottomInset` is true, so that a translucent keyboard shows the sheet's surface instead of the scaffold's background color.
3+
version: patch

0 commit comments

Comments
 (0)