From 680f33d238f1ee79a99435d99eadb18994dab775 Mon Sep 17 00:00:00 2001 From: Mairramer <50643541+Mairramer@users.noreply.github.com> Date: Thu, 29 Aug 2024 20:06:20 -0300 Subject: [PATCH] fix:: elevation and scrollUnderElevation depends on scrolling drawers on issue #120083 (#150793) fixes https://github.com/flutter/flutter/issues/120083 Description This PR addresses the following issues: - Resolves the unexpected scrolledUnderElevation trigger that occurs when scrolling within the Drawer and NavigationDrawer. --- .../flutter/lib/src/material/app_bar.dart | 5 + .../flutter/test/material/app_bar_test.dart | 150 ++++++++++++++++++ 2 files changed, 155 insertions(+) diff --git a/packages/flutter/lib/src/material/app_bar.dart b/packages/flutter/lib/src/material/app_bar.dart index 0d0ff4f88e..697ddc16de 100644 --- a/packages/flutter/lib/src/material/app_bar.dart +++ b/packages/flutter/lib/src/material/app_bar.dart @@ -775,6 +775,11 @@ class _AppBarState extends State { void didChangeDependencies() { super.didChangeDependencies(); _scrollNotificationObserver?.removeListener(_handleScrollNotification); + final ScaffoldState? scaffoldState = Scaffold.maybeOf(context); + + if (scaffoldState != null && (scaffoldState.isDrawerOpen || scaffoldState.isEndDrawerOpen)) { + return; + } _scrollNotificationObserver = ScrollNotificationObserver.maybeOf(context); _scrollNotificationObserver?.addListener(_handleScrollNotification); } diff --git a/packages/flutter/test/material/app_bar_test.dart b/packages/flutter/test/material/app_bar_test.dart index 3278663a34..976ceb0b77 100644 --- a/packages/flutter/test/material/app_bar_test.dart +++ b/packages/flutter/test/material/app_bar_test.dart @@ -2563,6 +2563,156 @@ void main() { expect(getAppBarBackgroundColor(tester), defaultColor); expect(tester.getSize(findAppBarMaterial()).height, kToolbarHeight); }); + + testWidgets('scrolledUnderElevation should be maintained when drawer is opened', (WidgetTester tester) async { + final GlobalKey drawerListKey = GlobalKey(); + final GlobalKey bodyListKey = GlobalKey(); + await tester.pumpWidget(MaterialApp( + home: Scaffold( + appBar: AppBar( + elevation: 0, + backgroundColor: MaterialStateColor.resolveWith((Set states) { + return states.contains(MaterialState.scrolledUnder) ? scrolledColor : defaultColor; + }), + title: const Text('AppBar'), + ), + drawer: Drawer( + child: ListView( + key: drawerListKey, + children: [ + Container(height: 1200, color: Colors.red), + ], + ), + ), + body: ListView( + key: bodyListKey, + children: [ + Container(height: 1200, color: Colors.teal), + ], + ), + ), + )); + + // Initial state: AppBar should have the default color. + expect(getAppBarBackgroundColor(tester), defaultColor); + + // Scroll the list view. + await tester.drag(find.byKey(bodyListKey), const Offset(0, -300)); + await tester.pumpAndSettle(); + + // The AppBar should now have the scrolled color. + expect(getAppBarBackgroundColor(tester), scrolledColor); + + // Open the drawer. + await tester.tap(find.byIcon(Icons.menu)); + await tester.pumpAndSettle(); + + // The AppBar should still have the scrolled color. + expect(getAppBarBackgroundColor(tester), scrolledColor); + + // Scroll the list inside the drawer. + await tester.drag(find.byKey(drawerListKey), const Offset(0, -300)); + await tester.pumpAndSettle(); + + // The AppBar should still have the scrolled color. + expect(getAppBarBackgroundColor(tester), scrolledColor); + + // Scroll list inside the drawer back to the top. + await tester.drag(find.byKey(drawerListKey), const Offset(0, 300)); + await tester.pumpAndSettle(); + + // The AppBar should still have the scrolled color. + expect(getAppBarBackgroundColor(tester), scrolledColor); + + // Close the drawer using the Scaffold's method. + tester.state(find.byType(Scaffold)).closeDrawer(); + await tester.pumpAndSettle(); + + // The AppBar should still have the scrolled color. + expect(getAppBarBackgroundColor(tester), scrolledColor); + + // Scroll the list view back to the top. + await tester.drag(find.byKey(bodyListKey), const Offset(0, 300)); + await tester.pumpAndSettle(); + + // The AppBar should be back to the default color. + expect(getAppBarBackgroundColor(tester), defaultColor); + }); + + testWidgets('scrolledUnderElevation should be maintained when endDrawer is opened', (WidgetTester tester) async { + final GlobalKey drawerListKey = GlobalKey(); + final GlobalKey bodyListKey = GlobalKey(); + await tester.pumpWidget(MaterialApp( + home: Scaffold( + appBar: AppBar( + elevation: 0, + backgroundColor: MaterialStateColor.resolveWith((Set states) { + return states.contains(MaterialState.scrolledUnder) ? scrolledColor : defaultColor; + }), + title: const Text('AppBar'), + ), + endDrawer: Drawer( + child: ListView( + key: drawerListKey, + children: [ + Container(height: 1200, color: Colors.red), + ], + ), + ), + body: ListView( + key: bodyListKey, + children: [ + Container(height: 1200, color: Colors.teal), + ], + ), + ), + )); + + // Initial state: AppBar should have the default color. + expect(getAppBarBackgroundColor(tester), defaultColor); + + // Scroll the list view. + await tester.drag(find.byKey(bodyListKey), const Offset(0, -300)); + await tester.pumpAndSettle(); + + // The AppBar should now have the scrolled color. + expect(getAppBarBackgroundColor(tester), scrolledColor); + + // Open the drawer. + await tester.tap(find.byIcon(Icons.menu)); + await tester.pumpAndSettle(); + + // The AppBar should still have the scrolled color. + expect(getAppBarBackgroundColor(tester), scrolledColor); + + // Scroll the list inside the drawer. + await tester.drag(find.byKey(drawerListKey), const Offset(0, -300)); + await tester.pumpAndSettle(); + + // The AppBar should still have the scrolled color. + expect(getAppBarBackgroundColor(tester), scrolledColor); + + // Scroll list inside the drawer back to the top. + await tester.drag(find.byKey(drawerListKey), const Offset(0, 300)); + await tester.pumpAndSettle(); + + // The AppBar should still have the scrolled color. + expect(getAppBarBackgroundColor(tester), scrolledColor); + + // Close the drawer using the Scaffold's method. + tester.state(find.byType(Scaffold)).closeEndDrawer(); + await tester.pumpAndSettle(); + + // The AppBar should still have the scrolled color. + expect(getAppBarBackgroundColor(tester), scrolledColor); + + // Scroll the list view back to the top. + await tester.drag(find.byKey(bodyListKey), const Offset(0, 300)); + await tester.pumpAndSettle(); + + // The AppBar should be back to the default color. + expect(getAppBarBackgroundColor(tester), defaultColor); + }); }); // Regression test for https://github.com/flutter/flutter/issues/80256