From 328635b3dfa95e10ceab61c0487d68b4cb5e0a88 Mon Sep 17 00:00:00 2001 From: xubaolin Date: Wed, 12 Apr 2023 08:01:11 +0800 Subject: [PATCH] DraggableScrollableSheet & NestedScrollView should respect NeverScrollableScrollPhysics (#123109) DraggableScrollableSheet & NestedScrollView should respect NeverScrollableScrollPhysics --- .../widgets/draggable_scrollable_sheet.dart | 2 +- .../lib/src/widgets/nested_scroll_view.dart | 3 +- .../lib/src/widgets/scroll_physics.dart | 12 ++++- .../draggable_scrollable_sheet_test.dart | 45 +++++++++++++++++++ .../test/widgets/nested_scroll_view_test.dart | 41 +++++++++++++++++ 5 files changed, 99 insertions(+), 4 deletions(-) diff --git a/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart b/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart index a273d9b051..b9babfb2b1 100644 --- a/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart +++ b/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart @@ -781,7 +781,7 @@ class _DraggableScrollableSheetScrollController extends ScrollController { ScrollPosition? oldPosition, ) { return _DraggableScrollableSheetScrollPosition( - physics: const AlwaysScrollableScrollPhysics().applyTo(physics), + physics: physics.applyTo(const AlwaysScrollableScrollPhysics()), context: context, oldPosition: oldPosition, getExtent: () => extent, diff --git a/packages/flutter/lib/src/widgets/nested_scroll_view.dart b/packages/flutter/lib/src/widgets/nested_scroll_view.dart index 333e97bd64..2711e0b624 100644 --- a/packages/flutter/lib/src/widgets/nested_scroll_view.dart +++ b/packages/flutter/lib/src/widgets/nested_scroll_view.dart @@ -1439,7 +1439,8 @@ class _NestedScrollPosition extends ScrollPosition implements ScrollActivityDele } void updateCanDrag(double totalExtent) { - context.setCanDrag(totalExtent > (viewportDimension - maxScrollExtent) || minScrollExtent != maxScrollExtent); + context.setCanDrag(physics.allowUserScrolling && + (totalExtent > (viewportDimension - maxScrollExtent) || minScrollExtent != maxScrollExtent)); } @override diff --git a/packages/flutter/lib/src/widgets/scroll_physics.dart b/packages/flutter/lib/src/widgets/scroll_physics.dart index 8cb2fb05c8..59ee6fcc50 100644 --- a/packages/flutter/lib/src/widgets/scroll_physics.dart +++ b/packages/flutter/lib/src/widgets/scroll_physics.dart @@ -197,7 +197,8 @@ class ScrollPhysics { } /// Whether the scrollable should let the user adjust the scroll offset, for - /// example by dragging. + /// example by dragging. If [allowUserScrolling] is false, the scrollable + /// will never allow user input to change the scroll position. /// /// By default, the user can manipulate the scroll offset if, and only if, /// there is actually content outside the viewport to reveal. @@ -206,6 +207,10 @@ class ScrollPhysics { /// reference to it to use later, as the values may update, may not update, or /// may update to reflect an entirely unrelated scrollable. bool shouldAcceptUserOffset(ScrollMetrics position) { + if (!allowUserScrolling) { + return false; + } + if (parent == null) { return position.pixels != 0.0 || position.minScrollExtent != position.maxScrollExtent; } @@ -487,6 +492,9 @@ class ScrollPhysics { /// scroll position to fulfill such a request. bool get allowImplicitScrolling => true; + /// Whether a viewport is allowed to change the scroll position as the result of user input. + bool get allowUserScrolling => true; + @override String toString() { if (parent == null) { @@ -965,7 +973,7 @@ class NeverScrollableScrollPhysics extends ScrollPhysics { } @override - bool shouldAcceptUserOffset(ScrollMetrics position) => false; + bool get allowUserScrolling => false; @override bool get allowImplicitScrolling => false; diff --git a/packages/flutter/test/widgets/draggable_scrollable_sheet_test.dart b/packages/flutter/test/widgets/draggable_scrollable_sheet_test.dart index 63e067119a..994a28b4e4 100644 --- a/packages/flutter/test/widgets/draggable_scrollable_sheet_test.dart +++ b/packages/flutter/test/widgets/draggable_scrollable_sheet_test.dart @@ -1487,6 +1487,51 @@ void main() { ); }); + testWidgets('DraggableScrollableSheet should respect NeverScrollableScrollPhysics', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/121021 + final DraggableScrollableController controller = DraggableScrollableController(); + Widget buildFrame(ScrollPhysics? physics) { + return MaterialApp( + home: Scaffold( + body: DraggableScrollableSheet( + controller: controller, + initialChildSize: 0.25, + builder: (BuildContext context, ScrollController scrollController) { + return ListView( + physics: physics, + controller: scrollController, + children: [ + const Text('Drag me!'), + Container( + height: 10000.0, + color: Colors.blue, + ), + ], + ); + }, + ), + ), + ); + } + await tester.pumpWidget(buildFrame(const NeverScrollableScrollPhysics())); + + final double initPixels = controller.pixels; + + await tester.drag(find.text('Drag me!'), const Offset(0, -300)); + await tester.pumpAndSettle(); + + //Should not allow user scrolling. + expect(controller.pixels, initPixels); + + await tester.pumpWidget(buildFrame(null)); + + await tester.drag(find.text('Drag me!'), const Offset(0, -300.0)); + await tester.pumpAndSettle(); + + //Allow user scrolling. + expect(controller.pixels, initPixels + 300.0); + }); + testWidgets('DraggableScrollableSheet should not rebuild every frame while dragging', (WidgetTester tester) async { // Regression test for https://github.com/flutter/flutter/issues/67219 int buildCount = 0; diff --git a/packages/flutter/test/widgets/nested_scroll_view_test.dart b/packages/flutter/test/widgets/nested_scroll_view_test.dart index cc42bcd9cb..6c74a6513c 100644 --- a/packages/flutter/test/widgets/nested_scroll_view_test.dart +++ b/packages/flutter/test/widgets/nested_scroll_view_test.dart @@ -531,6 +531,47 @@ void main() { expect(point1.dy, greaterThan(point2.dy)); }); + testWidgets('NestedScrollViews respect NeverScrollableScrollPhysics', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/113753 + await tester.pumpWidget(Directionality( + textDirection: TextDirection.ltr, + child: Localizations( + locale: const Locale('en', 'US'), + delegates: const >[ + DefaultMaterialLocalizations.delegate, + DefaultWidgetsLocalizations.delegate, + ], + child: MediaQuery( + data: const MediaQueryData(), + child: NestedScrollView( + physics: const NeverScrollableScrollPhysics(), + headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) { + return [ + const SliverAppBar( + floating: true, + title: Text('AA'), + ), + ]; + }, + body: Container(), + ), + ), + ), + )); + + expect(find.text('AA'), findsOneWidget); + final Offset point1 = tester.getCenter(find.text('AA')); + + await tester.dragFrom(point1, const Offset(0.0, -200.0)); + await tester.pump(); + + final Offset point2 = tester.getCenter(find.text( + 'AA', + skipOffstage: false, + )); + expect(point1, point2); + }); + testWidgets('NestedScrollView and internal scrolling', (WidgetTester tester) async { debugDisableShadows = false; const List tabs = ['Hello', 'World'];