diff --git a/packages/flutter/lib/src/material/app_bar.dart b/packages/flutter/lib/src/material/app_bar.dart index 73acecff62..f7cc1bafeb 100644 --- a/packages/flutter/lib/src/material/app_bar.dart +++ b/packages/flutter/lib/src/material/app_bar.dart @@ -762,7 +762,9 @@ class _AppBarState extends State { void _handleScrollNotification(ScrollNotification notification) { if (notification is ScrollUpdateNotification) { final bool oldScrolledUnder = _scrolledUnder; - _scrolledUnder = notification.depth == 0 && notification.metrics.extentBefore > 0; + _scrolledUnder = notification.depth == 0 + && notification.metrics.extentBefore > 0 + && notification.metrics.axis == Axis.vertical; if (_scrolledUnder != oldScrolledUnder) { setState(() { // React to a change in MaterialState.scrolledUnder diff --git a/packages/flutter/test/material/app_bar_test.dart b/packages/flutter/test/material/app_bar_test.dart index cda709ed37..20ac61471f 100644 --- a/packages/flutter/test/material/app_bar_test.dart +++ b/packages/flutter/test/material/app_bar_test.dart @@ -2910,6 +2910,56 @@ void main() { expect(tester.takeException(), isNull); }); + testWidgets('AppBar scrolledUnder does not trigger on horizontal scroll', (WidgetTester tester) async { + const Color scrolledColor = Color(0xff00ff00); + const Color defaultColor = Color(0xff0000ff); + + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + appBar: AppBar( + backwardsCompatibility: false, + elevation: 0, + backgroundColor: MaterialStateColor.resolveWith((Set states) { + return states.contains(MaterialState.scrolledUnder) ? scrolledColor : defaultColor; + }), + title: const Text('AppBar'), + ), + body: ListView( + scrollDirection: Axis.horizontal, + children: [ + Container(height: 600.0, width: 1200.0, color: Colors.teal), + ], + ), + ), + ), + ); + + Finder findAppBarMaterial() { + return find.descendant(of: find.byType(AppBar), matching: find.byType(Material)); + } + + Color? getAppBarBackgroundColor() { + return tester.widget(findAppBarMaterial()).color; + } + + expect(getAppBarBackgroundColor(), defaultColor); + + TestGesture gesture = await tester.startGesture(const Offset(50.0, 400.0)); + await gesture.moveBy(const Offset(-100.0, 0.0)); + await gesture.up(); + await tester.pumpAndSettle(); + + expect(getAppBarBackgroundColor(), defaultColor); + + gesture = await tester.startGesture(const Offset(50.0, 400.0)); + await gesture.moveBy(const Offset(100.0, 0.0)); + await gesture.up(); + await tester.pumpAndSettle(); + + expect(getAppBarBackgroundColor(), defaultColor); + }); + testWidgets('AppBar.preferredHeightFor', (WidgetTester tester) async { late double preferredHeight; late Size preferredSize;