Fix Scaffold extend body (#157441)

Fixes: #157316
This commit is contained in:
yim 2024-10-27 12:54:30 +08:00 committed by GitHub
parent 030eea8617
commit 04bcfe9281
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 6 deletions

View File

@ -1087,17 +1087,21 @@ class _ScaffoldLayout extends MultiChildLayoutDelegate {
if (hasChild(_ScaffoldSlot.body)) {
double bodyMaxHeight = math.max(0.0, contentBottom - contentTop);
if (extendBody) {
// When extendBody is true, the body is visible underneath the bottom widgets.
// This does not apply when the area is obscured by the device keyboard.
if (extendBody && minInsets.bottom <= bottomWidgetsHeight) {
bodyMaxHeight += bottomWidgetsHeight;
bodyMaxHeight = clampDouble(bodyMaxHeight, 0.0, looseConstraints.maxHeight - contentTop);
assert(bodyMaxHeight <= math.max(0.0, looseConstraints.maxHeight - contentTop));
} else {
bottomWidgetsHeight = 0.0;
}
final BoxConstraints bodyConstraints = _BodyBoxConstraints(
maxWidth: fullWidthConstraints.maxWidth,
maxHeight: bodyMaxHeight,
materialBannerHeight: materialBannerSize.height,
bottomWidgetsHeight: extendBody ? bottomWidgetsHeight : 0.0,
bottomWidgetsHeight: bottomWidgetsHeight,
appBarHeight: appBarHeight,
);
layoutChild(_ScaffoldSlot.body, bodyConstraints);
@ -3086,9 +3090,6 @@ class ScaffoldState extends State<Scaffold> with TickerProviderStateMixin, Resto
bottom: _resizeToAvoidBottomInset && MediaQuery.viewInsetsOf(context).bottom != 0.0 ? 0.0 : null,
);
// extendBody locked when keyboard is open
final bool extendBody = minInsets.bottom <= 0 && widget.extendBody;
return _ScaffoldScope(
hasDrawer: hasDrawer,
geometryNotifier: _geometryNotifier,
@ -3102,7 +3103,7 @@ class ScaffoldState extends State<Scaffold> with TickerProviderStateMixin, Resto
},
child: CustomMultiChildLayout(
delegate: _ScaffoldLayout(
extendBody: extendBody,
extendBody: widget.extendBody,
extendBodyBehindAppBar: widget.extendBodyBehindAppBar,
minInsets: minInsets,
minViewPadding: minViewPadding,

View File

@ -3418,6 +3418,36 @@ void main() {
));
expect(scaffoldMaterial.color, theme.colorScheme.surface);
});
testWidgets('Body height remains Scaffold height when keyboard is smaller than bottomNavigationBar and extendBody is true', (WidgetTester tester) async {
final Key bodyKey = UniqueKey();
Widget buildFrame({double keyboardHeight = 0}) {
return MaterialApp(
home: Builder(
builder: (BuildContext context) {
return MediaQuery(
data: MediaQuery.of(context).copyWith(
viewInsets: EdgeInsets.only(bottom: keyboardHeight),
),
child: Scaffold(
extendBody: true,
body: SizedBox.expand(key: bodyKey),
bottomNavigationBar:const SizedBox(height: 100),
),
);
},
),
);
}
await tester.pumpWidget(buildFrame());
expect(tester.getSize(find.byKey(bodyKey)).height, 600);
await tester.pumpWidget(buildFrame(keyboardHeight: 100));
expect(tester.getSize(find.byKey(bodyKey)).height, 600);
await tester.pumpWidget(buildFrame(keyboardHeight: 200));
expect(tester.getSize(find.byKey(bodyKey)).height, 400);
});
}
class _GeometryListener extends StatefulWidget {