From 4f2211b550daef2d6073bb36542040f1f7156d51 Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Tue, 15 Oct 2024 13:58:51 -0700 Subject: [PATCH] Determine the scroll gesture axis based on the effectiveScrollController (#156190) Fixes https://github.com/flutter/flutter/issues/154520 --- .../flutter/lib/src/widgets/scrollbar.dart | 4 +- .../flutter/test/widgets/scrollbar_test.dart | 64 +++++++++++++++++++ 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/widgets/scrollbar.dart b/packages/flutter/lib/src/widgets/scrollbar.dart index 2c256c4aa5..adc181d57b 100644 --- a/packages/flutter/lib/src/widgets/scrollbar.dart +++ b/packages/flutter/lib/src/widgets/scrollbar.dart @@ -1993,7 +1993,7 @@ class RawScrollbarState extends State with TickerProv return gestures; } - switch (_axis) { + switch (_effectiveScrollController!.position.axis) { case Axis.horizontal: gestures[_HorizontalThumbDragGestureRecognizer] = GestureRecognizerFactoryWithHandlers<_HorizontalThumbDragGestureRecognizer>( @@ -2012,8 +2012,6 @@ class RawScrollbarState extends State with TickerProv ), _initThumbDragGestureRecognizer, ); - case null: - return gestures; } gestures[_TrackTapGestureRecognizer] = diff --git a/packages/flutter/test/widgets/scrollbar_test.dart b/packages/flutter/test/widgets/scrollbar_test.dart index 16df9d4cdb..01695da777 100644 --- a/packages/flutter/test/widgets/scrollbar_test.dart +++ b/packages/flutter/test/widgets/scrollbar_test.dart @@ -3489,4 +3489,68 @@ The provided ScrollController cannot be shared by multiple ScrollView widgets.'' // Scrolling is now possible, so there are scrollbar (thumb and track) gesture recognizers. expect(getScrollbarGestureDetector().gestures.length, greaterThan(1)); }); + + testWidgets('Drag horizontal and vertical scrollbars', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/87697 + final ScrollController verticalScrollController = ScrollController(); + addTearDown(verticalScrollController.dispose); + final ScrollController horizontalScrollController = ScrollController(); + addTearDown(horizontalScrollController.dispose); + + final GlobalKey key1 = GlobalKey(); + Widget buildFrame() { + return Directionality( + textDirection: TextDirection.ltr, + child: MediaQuery( + data: const MediaQueryData(), + child: RawScrollbar( + thumbVisibility: true, + controller: verticalScrollController, + child: RawScrollbar( + thumbVisibility: true, + controller: horizontalScrollController, + key: key1, + notificationPredicate: (ScrollNotification notification) => notification.depth == 1, + child: SingleChildScrollView( + controller: verticalScrollController, + child: SingleChildScrollView( + scrollDirection: Axis.horizontal, + controller: horizontalScrollController, + child: const SizedBox( + width: 1000.0, + height: 1000.0, + ), + ), + ), + ), + ), + ), + ); + } + + await tester.pumpWidget(buildFrame()); + await tester.pumpAndSettle(); + + expect(verticalScrollController.offset, 0.0); + expect(horizontalScrollController.offset, 0.0); + + const double scrollAmount = 10.0; + + final TestGesture verticalScrollbarGesture = await tester.startGesture(const Offset(797.0, 45.0)); + await tester.pumpAndSettle(); + await verticalScrollbarGesture.moveBy(const Offset(0.0, scrollAmount)); + await tester.pumpAndSettle(); + await verticalScrollbarGesture.up(); + await tester.pumpAndSettle(); + + final TestGesture horizontalScrollbarGesture = await tester.startGesture(const Offset(45.0, 597.0)); + await tester.pumpAndSettle(); + await horizontalScrollbarGesture.moveBy(const Offset(scrollAmount, 0.0)); + await tester.pumpAndSettle(); + await horizontalScrollbarGesture.up(); + await tester.pumpAndSettle(); + + expect(verticalScrollController.offset, greaterThan(0.0)); + expect(horizontalScrollController.offset, greaterThan(0.0)); + }); }