From caadc255b7323529b7cadb3cbb54afa235b83b53 Mon Sep 17 00:00:00 2001 From: xubaolin Date: Thu, 30 Jun 2022 10:50:05 +0800 Subject: [PATCH] Reland "fix a Scaffold extendBodyBehindAppBar update bug" (#106534) --- .../flutter/lib/src/material/scaffold.dart | 4 ++- .../flutter/test/material/scaffold_test.dart | 34 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/material/scaffold.dart b/packages/flutter/lib/src/material/scaffold.dart index 5be81b2ac2..7757cf1025 100644 --- a/packages/flutter/lib/src/material/scaffold.dart +++ b/packages/flutter/lib/src/material/scaffold.dart @@ -1958,6 +1958,8 @@ class ScaffoldState extends State with TickerProviderStateMixin, Resto final GlobalKey _drawerKey = GlobalKey(); final GlobalKey _endDrawerKey = GlobalKey(); + final GlobalKey _bodyKey = GlobalKey(); + /// Whether this scaffold has a non-null [Scaffold.appBar]. bool get hasAppBar => widget.appBar != null; /// Whether this scaffold has a non-null [Scaffold.drawer]. @@ -2653,7 +2655,7 @@ class ScaffoldState extends State with TickerProviderStateMixin, Resto widget.body == null ? null : _BodyBuilder( extendBody: widget.extendBody, extendBodyBehindAppBar: widget.extendBodyBehindAppBar, - body: widget.body!, + body: KeyedSubtree(key: _bodyKey, child: widget.body!), ), _ScaffoldSlot.body, removeLeftPadding: false, diff --git a/packages/flutter/test/material/scaffold_test.dart b/packages/flutter/test/material/scaffold_test.dart index b970288a2d..d39ce30f0e 100644 --- a/packages/flutter/test/material/scaffold_test.dart +++ b/packages/flutter/test/material/scaffold_test.dart @@ -12,6 +12,39 @@ import 'package:flutter_test/flutter_test.dart'; import '../widgets/semantics_tester.dart'; void main() { + // Regression test for https://github.com/flutter/flutter/issues/103741 + testWidgets('extendBodyBehindAppBar change should not cause the body widget lose state', (WidgetTester tester) async { + final ScrollController controller = ScrollController(); + Widget buildFrame({required bool extendBodyBehindAppBar}) { + return MediaQuery( + data: const MediaQueryData(), + child: Directionality( + textDirection: TextDirection.ltr, + child: Scaffold( + extendBodyBehindAppBar: extendBodyBehindAppBar, + resizeToAvoidBottomInset: false, + body: SingleChildScrollView( + controller: controller, + child: const FlutterLogo( + size: 1107, + ), + ), + ), + ), + ); + } + + await tester.pumpWidget(buildFrame(extendBodyBehindAppBar: true)); + expect(controller.position.pixels, 0.0); + + controller.jumpTo(100.0); + await tester.pump(); + expect(controller.position.pixels, 100.0); + + await tester.pumpWidget(buildFrame(extendBodyBehindAppBar: false)); + expect(controller.position.pixels, 100.0); + }); + testWidgets('Scaffold drawer callback test', (WidgetTester tester) async { bool isDrawerOpen = false; bool isEndDrawerOpen = false; @@ -2402,6 +2435,7 @@ void main() { ' ancestor was:\n' ' Builder\n' ' The ancestors of this widget were:\n' + ' KeyedSubtree-[GlobalKey#00000]\n' ' _BodyBuilder\n' ' MediaQuery\n' ' LayoutId-[<_ScaffoldSlot.body>]\n'