Skip to content

Commit 680f33d

Browse files
authored
fix:: elevation and scrollUnderElevation depends on scrolling drawers on issue flutter#120083 (flutter#150793)
fixes flutter#120083 Description This PR addresses the following issues: - Resolves the unexpected scrolledUnderElevation trigger that occurs when scrolling within the Drawer and NavigationDrawer.
1 parent 712cf65 commit 680f33d

File tree

2 files changed

+155
-0
lines changed

2 files changed

+155
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,11 @@ class _AppBarState extends State<AppBar> {
775775
void didChangeDependencies() {
776776
super.didChangeDependencies();
777777
_scrollNotificationObserver?.removeListener(_handleScrollNotification);
778+
final ScaffoldState? scaffoldState = Scaffold.maybeOf(context);
779+
780+
if (scaffoldState != null && (scaffoldState.isDrawerOpen || scaffoldState.isEndDrawerOpen)) {
781+
return;
782+
}
778783
_scrollNotificationObserver = ScrollNotificationObserver.maybeOf(context);
779784
_scrollNotificationObserver?.addListener(_handleScrollNotification);
780785
}

packages/flutter/test/material/app_bar_test.dart

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2563,6 +2563,156 @@ void main() {
25632563
expect(getAppBarBackgroundColor(tester), defaultColor);
25642564
expect(tester.getSize(findAppBarMaterial()).height, kToolbarHeight);
25652565
});
2566+
2567+
testWidgets('scrolledUnderElevation should be maintained when drawer is opened', (WidgetTester tester) async {
2568+
final GlobalKey drawerListKey = GlobalKey();
2569+
final GlobalKey bodyListKey = GlobalKey();
2570+
await tester.pumpWidget(MaterialApp(
2571+
home: Scaffold(
2572+
appBar: AppBar(
2573+
elevation: 0,
2574+
backgroundColor: MaterialStateColor.resolveWith((Set<MaterialState> states) {
2575+
return states.contains(MaterialState.scrolledUnder) ? scrolledColor : defaultColor;
2576+
}),
2577+
title: const Text('AppBar'),
2578+
),
2579+
drawer: Drawer(
2580+
child: ListView(
2581+
key: drawerListKey,
2582+
children: <Widget>[
2583+
Container(height: 1200, color: Colors.red),
2584+
],
2585+
),
2586+
),
2587+
body: ListView(
2588+
key: bodyListKey,
2589+
children: <Widget>[
2590+
Container(height: 1200, color: Colors.teal),
2591+
],
2592+
),
2593+
),
2594+
));
2595+
2596+
// Initial state: AppBar should have the default color.
2597+
expect(getAppBarBackgroundColor(tester), defaultColor);
2598+
2599+
// Scroll the list view.
2600+
await tester.drag(find.byKey(bodyListKey), const Offset(0, -300));
2601+
await tester.pumpAndSettle();
2602+
2603+
// The AppBar should now have the scrolled color.
2604+
expect(getAppBarBackgroundColor(tester), scrolledColor);
2605+
2606+
// Open the drawer.
2607+
await tester.tap(find.byIcon(Icons.menu));
2608+
await tester.pumpAndSettle();
2609+
2610+
// The AppBar should still have the scrolled color.
2611+
expect(getAppBarBackgroundColor(tester), scrolledColor);
2612+
2613+
// Scroll the list inside the drawer.
2614+
await tester.drag(find.byKey(drawerListKey), const Offset(0, -300));
2615+
await tester.pumpAndSettle();
2616+
2617+
// The AppBar should still have the scrolled color.
2618+
expect(getAppBarBackgroundColor(tester), scrolledColor);
2619+
2620+
// Scroll list inside the drawer back to the top.
2621+
await tester.drag(find.byKey(drawerListKey), const Offset(0, 300));
2622+
await tester.pumpAndSettle();
2623+
2624+
// The AppBar should still have the scrolled color.
2625+
expect(getAppBarBackgroundColor(tester), scrolledColor);
2626+
2627+
// Close the drawer using the Scaffold's method.
2628+
tester.state<ScaffoldState>(find.byType(Scaffold)).closeDrawer();
2629+
await tester.pumpAndSettle();
2630+
2631+
// The AppBar should still have the scrolled color.
2632+
expect(getAppBarBackgroundColor(tester), scrolledColor);
2633+
2634+
// Scroll the list view back to the top.
2635+
await tester.drag(find.byKey(bodyListKey), const Offset(0, 300));
2636+
await tester.pumpAndSettle();
2637+
2638+
// The AppBar should be back to the default color.
2639+
expect(getAppBarBackgroundColor(tester), defaultColor);
2640+
});
2641+
2642+
testWidgets('scrolledUnderElevation should be maintained when endDrawer is opened', (WidgetTester tester) async {
2643+
final GlobalKey drawerListKey = GlobalKey();
2644+
final GlobalKey bodyListKey = GlobalKey();
2645+
await tester.pumpWidget(MaterialApp(
2646+
home: Scaffold(
2647+
appBar: AppBar(
2648+
elevation: 0,
2649+
backgroundColor: MaterialStateColor.resolveWith((Set<MaterialState> states) {
2650+
return states.contains(MaterialState.scrolledUnder) ? scrolledColor : defaultColor;
2651+
}),
2652+
title: const Text('AppBar'),
2653+
),
2654+
endDrawer: Drawer(
2655+
child: ListView(
2656+
key: drawerListKey,
2657+
children: <Widget>[
2658+
Container(height: 1200, color: Colors.red),
2659+
],
2660+
),
2661+
),
2662+
body: ListView(
2663+
key: bodyListKey,
2664+
children: <Widget>[
2665+
Container(height: 1200, color: Colors.teal),
2666+
],
2667+
),
2668+
),
2669+
));
2670+
2671+
// Initial state: AppBar should have the default color.
2672+
expect(getAppBarBackgroundColor(tester), defaultColor);
2673+
2674+
// Scroll the list view.
2675+
await tester.drag(find.byKey(bodyListKey), const Offset(0, -300));
2676+
await tester.pumpAndSettle();
2677+
2678+
// The AppBar should now have the scrolled color.
2679+
expect(getAppBarBackgroundColor(tester), scrolledColor);
2680+
2681+
// Open the drawer.
2682+
await tester.tap(find.byIcon(Icons.menu));
2683+
await tester.pumpAndSettle();
2684+
2685+
// The AppBar should still have the scrolled color.
2686+
expect(getAppBarBackgroundColor(tester), scrolledColor);
2687+
2688+
// Scroll the list inside the drawer.
2689+
await tester.drag(find.byKey(drawerListKey), const Offset(0, -300));
2690+
await tester.pumpAndSettle();
2691+
2692+
// The AppBar should still have the scrolled color.
2693+
expect(getAppBarBackgroundColor(tester), scrolledColor);
2694+
2695+
// Scroll list inside the drawer back to the top.
2696+
await tester.drag(find.byKey(drawerListKey), const Offset(0, 300));
2697+
await tester.pumpAndSettle();
2698+
2699+
// The AppBar should still have the scrolled color.
2700+
expect(getAppBarBackgroundColor(tester), scrolledColor);
2701+
2702+
// Close the drawer using the Scaffold's method.
2703+
tester.state<ScaffoldState>(find.byType(Scaffold)).closeEndDrawer();
2704+
await tester.pumpAndSettle();
2705+
2706+
// The AppBar should still have the scrolled color.
2707+
expect(getAppBarBackgroundColor(tester), scrolledColor);
2708+
2709+
// Scroll the list view back to the top.
2710+
await tester.drag(find.byKey(bodyListKey), const Offset(0, 300));
2711+
await tester.pumpAndSettle();
2712+
2713+
// The AppBar should be back to the default color.
2714+
expect(getAppBarBackgroundColor(tester), defaultColor);
2715+
});
25662716
});
25672717

25682718
// Regression test for https://github.com/flutter/flutter/issues/80256

0 commit comments

Comments
 (0)