Skip to content

Commit 70dac41

Browse files
committed
Add BottomSheet.bottomInset instead of @internal API
1 parent 604206b commit 70dac41

3 files changed

Lines changed: 127 additions & 42 deletions

File tree

‎packages/flutter/lib/src/material/bottom_sheet.dart‎

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,6 @@ const double _kMinFlingVelocity = 700.0;
3131
const double _kCloseProgressThreshold = 0.5;
3232
const double _kDefaultScrollControlDisabledMaxHeightRatio = 9.0 / 16.0;
3333

34-
/// Internal coordination between [Scaffold] and [BottomSheet].
35-
///
36-
/// Only persistent sheets receive an extension. Modal sheets and standalone
37-
/// [BottomSheet]s retain their existing layout policy.
38-
@internal
39-
class BottomSheetKeyboardInset extends InheritedWidget {
40-
/// Supplies the extra surface extent computed by [Scaffold].
41-
const BottomSheetKeyboardInset({super.key, required this.bottom, required super.child});
42-
43-
/// The distance below the usable sheet area, in logical pixels.
44-
final double bottom;
45-
46-
/// Returns zero outside a persistent sheet or below its content boundary.
47-
static double of(BuildContext context) =>
48-
context.dependOnInheritedWidgetOfExactType<BottomSheetKeyboardInset>()?.bottom ?? 0.0;
49-
50-
@override
51-
bool updateShouldNotify(BottomSheetKeyboardInset oldWidget) => bottom != oldWidget.bottom;
52-
}
53-
5434
/// A callback for when the user begins dragging the bottom sheet.
5535
///
5636
/// Used by [BottomSheet.onDragStart].
@@ -113,9 +93,11 @@ class BottomSheet extends StatefulWidget {
11393
this.shape,
11494
this.clipBehavior,
11595
this.constraints,
96+
this.bottomInset = 0.0,
11697
required this.onClosing,
11798
required this.builder,
118-
}) : assert(elevation == null || elevation >= 0.0);
99+
}) : assert(elevation == null || elevation >= 0.0),
100+
assert(bottomInset >= 0.0 && bottomInset < double.infinity);
119101

120102
/// The animation controller that controls the bottom sheet's entrance and
121103
/// exit animations.
@@ -256,6 +238,21 @@ class BottomSheet extends StatefulWidget {
256238
/// the available space. Otherwise, no alignment is applied.
257239
final BoxConstraints? constraints;
258240

241+
/// Additional space below the content and drag handle, painted by the sheet's
242+
/// [Material] background.
243+
///
244+
/// This space is added to the height [constraints], preserving the space
245+
/// available to the content, subject to the parent's constraints. It does not
246+
/// add any width or read the ambient [MediaQuery].
247+
///
248+
/// [Scaffold] sets this when a persistent sheet avoids the keyboard. Custom
249+
/// presentations can also use it to extend the surface while keeping content
250+
/// above an inset. The presentation is responsible for positioning the sheet
251+
/// and keeping the inset area out of hit testing and accessibility bounds.
252+
///
253+
/// Defaults to zero. Must be finite and non-negative.
254+
final double bottomInset;
255+
259256
@override
260257
State<BottomSheet> createState() => _BottomSheetState();
261258

@@ -375,7 +372,7 @@ class _BottomSheetState extends State<BottomSheet> {
375372
// Scaffold reserves this space for the persistent sheet's surface. Inflate
376373
// height constraints before adding padding so the builder retains its
377374
// original constraints, including explicit or themed min/max heights.
378-
final double keyboardInset = BottomSheetKeyboardInset.of(context);
375+
final double keyboardInset = widget.bottomInset;
379376
final BoxConstraints? contentConstraints =
380377
widget.constraints ?? bottomSheetTheme.constraints ?? defaults.constraints;
381378
final BoxConstraints? constraints = contentConstraints?.copyWith(
@@ -426,23 +423,20 @@ class _BottomSheetState extends State<BottomSheet> {
426423
clipBehavior: clipBehavior,
427424
child: Padding(
428425
padding: EdgeInsets.only(bottom: keyboardInset),
429-
child: BottomSheetKeyboardInset(
430-
bottom: 0.0,
431-
child: NotificationListener<DraggableScrollableNotification>(
432-
onNotification: extentChanged,
433-
child: !showDragHandle
434-
? widget.builder(context)
435-
: Stack(
436-
alignment: Alignment.topCenter,
437-
children: <Widget>[
438-
dragHandle!,
439-
Padding(
440-
padding: const EdgeInsets.only(top: kMinInteractiveDimension),
441-
child: widget.builder(context),
442-
),
443-
],
444-
),
445-
),
426+
child: NotificationListener<DraggableScrollableNotification>(
427+
onNotification: extentChanged,
428+
child: !showDragHandle
429+
? widget.builder(context)
430+
: Stack(
431+
alignment: Alignment.topCenter,
432+
children: <Widget>[
433+
dragHandle!,
434+
Padding(
435+
padding: const EdgeInsets.only(top: kMinInteractiveDimension),
436+
child: widget.builder(context),
437+
),
438+
],
439+
),
446440
),
447441
),
448442
);

‎packages/flutter/lib/src/material/scaffold.dart‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ import 'dart:ui';
2222

2323
import 'package:flutter/foundation.dart';
2424
import 'package:flutter/gestures.dart' show DragStartBehavior, HitTestEntry, HitTestResult;
25-
import 'package:flutter/rendering.dart'
26-
show BoxHitTestResult, RenderBox, RenderMetaData, RenderProxyBox;
25+
import 'package:flutter/rendering.dart';
2726
import 'package:flutter/widgets.dart';
2827

2928
import 'app_bar.dart';
@@ -3102,7 +3101,7 @@ class ScaffoldState extends State<Scaffold>
31023101
final Widget stack = LayoutBuilder(
31033102
builder: (BuildContext context, BoxConstraints constraints) {
31043103
final double keyboardInset = (constraints as _BottomSheetConstraints).keyboardInset;
3105-
return BottomSheetKeyboardInset(
3104+
return _BottomSheetKeyboardInset(
31063105
bottom: keyboardInset,
31073106
child: ClipRect(
31083107
clipBehavior: keyboardInset > 0.0 ? Clip.hardEdge : Clip.none,
@@ -3350,6 +3349,20 @@ class ScaffoldFeatureController<T extends Widget, U> {
33503349
final StateSetter? setState;
33513350
}
33523351

3352+
// Carries Scaffold's layout result to its persistent sheet. BottomSheet receives
3353+
// the value explicitly; standalone and nested sheets do not inherit it.
3354+
class _BottomSheetKeyboardInset extends InheritedWidget {
3355+
const _BottomSheetKeyboardInset({required this.bottom, required super.child});
3356+
3357+
final double bottom;
3358+
3359+
static double of(BuildContext context) =>
3360+
context.dependOnInheritedWidgetOfExactType<_BottomSheetKeyboardInset>()!.bottom;
3361+
3362+
@override
3363+
bool updateShouldNotify(_BottomSheetKeyboardInset oldWidget) => bottom != oldWidget.bottom;
3364+
}
3365+
33533366
// Passed directly to the bottom-sheet LayoutBuilder so inset-only changes also
33543367
// trigger layout when the total surface constraints remain unchanged.
33553368
class _BottomSheetConstraints extends BoxConstraints {
@@ -3554,6 +3567,7 @@ class _StandardBottomSheetState extends State<_StandardBottomSheet> {
35543567
child: NotificationListener<DraggableScrollableNotification>(
35553568
onNotification: extentChanged,
35563569
child: BottomSheet(
3570+
bottomInset: _BottomSheetKeyboardInset.of(context),
35573571
animationController: widget.animationController,
35583572
enableDrag: widget.enableDrag,
35593573
showDragHandle: widget.showDragHandle,

‎packages/flutter/test/material/bottom_sheet_test.dart‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,83 @@ void main() {
2727
expect(dyDelta1, isNot(moreOrLessEquals(dyDelta2, epsilon: 0.1)));
2828
}
2929

30+
test('BottomSheet.bottomInset defaults to zero and rejects invalid extents', () {
31+
expect(BottomSheet(onClosing: () {}, builder: (_) => const SizedBox()).bottomInset, 0.0);
32+
for (final inset in <double>[-1.0, double.infinity, double.nan]) {
33+
expect(
34+
() => BottomSheet(bottomInset: inset, onClosing: () {}, builder: (_) => const SizedBox()),
35+
throwsAssertionError,
36+
);
37+
}
38+
});
39+
40+
for (final themed in <bool>[false, true]) {
41+
testWidgets(
42+
'explicit bottomInset preserves content constraints with themed=$themed constraints',
43+
(WidgetTester tester) async {
44+
const contentKey = ValueKey<String>('inset content');
45+
const constraints = BoxConstraints.tightFor(width: 240.0, height: 150.0);
46+
for (final inset in <double>[0.0, 200.0, 60.0, 0.0]) {
47+
await tester.pumpWidget(
48+
MaterialApp(
49+
theme: ThemeData(
50+
bottomSheetTheme: BottomSheetThemeData(constraints: themed ? constraints : null),
51+
),
52+
home: MediaQuery(
53+
// Standalone sheets must use their explicit inset, not the ambient
54+
// keyboard geometry or a Scaffold's private inherited value.
55+
data: const MediaQueryData(viewInsets: EdgeInsets.only(bottom: 400.0)),
56+
child: Align(
57+
child: BottomSheet(
58+
bottomInset: inset,
59+
constraints: themed ? null : constraints,
60+
enableDrag: false,
61+
onClosing: () {},
62+
builder: (_) => const SizedBox.expand(key: contentKey),
63+
),
64+
),
65+
),
66+
),
67+
);
68+
await tester.pumpAndSettle();
69+
final Finder material = find.descendant(
70+
of: find.byType(BottomSheet),
71+
matching: find.byType(Material),
72+
);
73+
expect(tester.getSize(find.byKey(contentKey)), const Size(240.0, 150.0));
74+
expect(tester.getSize(material), Size(240.0, 150.0 + inset));
75+
expect(
76+
tester.getBottomLeft(material).dy - tester.getBottomLeft(find.byKey(contentKey)).dy,
77+
inset,
78+
);
79+
}
80+
},
81+
);
82+
}
83+
84+
testWidgets('bottomInset remains subject to parent constraints', (WidgetTester tester) async {
85+
const contentKey = ValueKey<String>('constrained inset content');
86+
await tester.pumpWidget(
87+
MaterialApp(
88+
home: Center(
89+
child: SizedBox(
90+
width: 240.0,
91+
height: 250.0,
92+
child: BottomSheet(
93+
bottomInset: 200.0,
94+
constraints: const BoxConstraints.tightFor(height: 150.0),
95+
enableDrag: false,
96+
onClosing: () {},
97+
builder: (_) => const SizedBox.expand(key: contentKey),
98+
),
99+
),
100+
),
101+
),
102+
);
103+
expect(tester.getSize(find.byKey(contentKey)), const Size(240.0, 50.0));
104+
expect(tester.takeException(), isNull);
105+
});
106+
30107
testWidgets('Throw if enable drag without an animation controller', (WidgetTester tester) async {
31108
// Regression test for https://github.com/flutter/flutter/issues/89168
32109
await tester.pumpWidget(

0 commit comments

Comments
 (0)