Skip to content

Commit ea28f28

Browse files
authored
fix a Scaffold extendBodyBehindAppBar update bug (#104958)
1 parent 512e090 commit ea28f28

File tree

4 files changed

+70
-45
lines changed

4 files changed

+70
-45
lines changed

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -875,10 +875,6 @@ class _BodyBuilder extends StatelessWidget {
875875

876876
@override
877877
Widget build(BuildContext context) {
878-
if (!extendBody && !extendBodyBehindAppBar) {
879-
return body;
880-
}
881-
882878
return LayoutBuilder(
883879
builder: (BuildContext context, BoxConstraints constraints) {
884880
final _BodyBoxConstraints bodyConstraints = constraints as _BodyBoxConstraints;

packages/flutter/test/material/scaffold_test.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,39 @@ import 'package:flutter_test/flutter_test.dart';
1111
import '../widgets/semantics_tester.dart';
1212

1313
void main() {
14+
// Regression test for https://github.com/flutter/flutter/issues/103741
15+
testWidgets('extendBodyBehindAppBar change should not cause the body widget lose state', (WidgetTester tester) async {
16+
final ScrollController controller = ScrollController();
17+
Widget buildFrame({required bool extendBodyBehindAppBar}) {
18+
return MediaQuery(
19+
data: const MediaQueryData(),
20+
child: Directionality(
21+
textDirection: TextDirection.ltr,
22+
child: Scaffold(
23+
extendBodyBehindAppBar: extendBodyBehindAppBar,
24+
resizeToAvoidBottomInset: false,
25+
body: SingleChildScrollView(
26+
controller: controller,
27+
child: const FlutterLogo(
28+
size: 1107,
29+
),
30+
),
31+
),
32+
),
33+
);
34+
}
35+
36+
await tester.pumpWidget(buildFrame(extendBodyBehindAppBar: true));
37+
expect(controller.position.pixels, 0.0);
38+
39+
controller.jumpTo(100.0);
40+
await tester.pump();
41+
expect(controller.position.pixels, 100.0);
42+
43+
await tester.pumpWidget(buildFrame(extendBodyBehindAppBar: false));
44+
expect(controller.position.pixels, 100.0);
45+
});
46+
1447
testWidgets('Scaffold drawer callback test', (WidgetTester tester) async {
1548
bool isDrawerOpen = false;
1649
bool isEndDrawerOpen = false;
@@ -2401,6 +2434,8 @@ void main() {
24012434
' ancestor was:\n'
24022435
' Builder\n'
24032436
' The ancestors of this widget were:\n'
2437+
' MediaQuery\n'
2438+
' LayoutBuilder\n'
24042439
' _BodyBuilder\n'
24052440
' MediaQuery\n'
24062441
' LayoutId-[<_ScaffoldSlot.body>]\n'

packages/flutter/test/widgets/interactive_viewer_test.dart

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,11 +1269,9 @@ void main() {
12691269
testWidgets('LayoutBuilder is only used for InteractiveViewer.builder', (WidgetTester tester) async {
12701270
await tester.pumpWidget(
12711271
MaterialApp(
1272-
home: Scaffold(
1273-
body: Center(
1274-
child: InteractiveViewer(
1275-
child: const SizedBox(width: 200.0, height: 200.0),
1276-
),
1272+
home: Center(
1273+
child: InteractiveViewer(
1274+
child: const SizedBox(width: 200.0, height: 200.0),
12771275
),
12781276
),
12791277
),
@@ -1283,13 +1281,11 @@ void main() {
12831281

12841282
await tester.pumpWidget(
12851283
MaterialApp(
1286-
home: Scaffold(
1287-
body: Center(
1288-
child: InteractiveViewer.builder(
1289-
builder: (BuildContext context, Quad viewport) {
1290-
return const SizedBox(width: 200.0, height: 200.0);
1291-
},
1292-
),
1284+
home: Center(
1285+
child: InteractiveViewer.builder(
1286+
builder: (BuildContext context, Quad viewport) {
1287+
return const SizedBox(width: 200.0, height: 200.0);
1288+
},
12931289
),
12941290
),
12951291
),

packages/flutter/test/widgets/nested_scroll_view_test.dart

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2360,37 +2360,35 @@ void main() {
23602360
testWidgets('NestedScrollView works well when rebuilding during scheduleWarmUpFrame', (WidgetTester tester) async {
23612361
bool? isScrolled;
23622362
final Widget myApp = MaterialApp(
2363-
home: Scaffold(
2364-
body: StatefulBuilder(
2365-
builder: (BuildContext context, StateSetter setState) {
2366-
return Focus(
2367-
onFocusChange: (_) => setState( (){} ),
2368-
child: NestedScrollView(
2369-
headerSliverBuilder: (BuildContext context, bool boxIsScrolled) {
2370-
isScrolled = boxIsScrolled;
2371-
return <Widget>[
2372-
const SliverAppBar(
2373-
expandedHeight: 200,
2374-
title: Text('Test'),
2375-
),
2376-
];
2377-
},
2378-
body: CustomScrollView(
2379-
slivers: <Widget>[
2380-
SliverList(
2381-
delegate: SliverChildBuilderDelegate(
2382-
(BuildContext context, int index) {
2383-
return const Text('');
2384-
},
2385-
childCount: 10,
2386-
),
2363+
home: StatefulBuilder(
2364+
builder: (BuildContext context, StateSetter setState) {
2365+
return Focus(
2366+
onFocusChange: (_) => setState( (){} ),
2367+
child: NestedScrollView(
2368+
headerSliverBuilder: (BuildContext context, bool boxIsScrolled) {
2369+
isScrolled = boxIsScrolled;
2370+
return <Widget>[
2371+
const SliverAppBar(
2372+
expandedHeight: 200,
2373+
title: Text('Test'),
2374+
),
2375+
];
2376+
},
2377+
body: CustomScrollView(
2378+
slivers: <Widget>[
2379+
SliverList(
2380+
delegate: SliverChildBuilderDelegate(
2381+
(BuildContext context, int index) {
2382+
return const Text('');
2383+
},
2384+
childCount: 10,
23872385
),
2388-
],
2389-
),
2386+
),
2387+
],
23902388
),
2391-
);
2392-
},
2393-
),
2389+
),
2390+
);
2391+
},
23942392
),
23952393
);
23962394

0 commit comments

Comments
 (0)