diff --git a/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart b/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart index 305601e34bd..e597749b781 100644 --- a/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart +++ b/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart @@ -1185,6 +1185,10 @@ class _SnappingSimulation extends Simulation { return pixelSnapSizes.first; } final double nextSize = pixelSnapSizes[indexOfNextSize]; + // If already snapped - keep this as target size + if (nextSize == position) { + return nextSize; + } final double previousSize = pixelSnapSizes[indexOfNextSize - 1]; if (initialVelocity.abs() <= tolerance.velocity) { // If velocity is zero, snap to the nearest snap size with the minimum velocity. diff --git a/packages/flutter/test/widgets/draggable_scrollable_sheet_test.dart b/packages/flutter/test/widgets/draggable_scrollable_sheet_test.dart index 27dfdb90d8e..7ab5c461893 100644 --- a/packages/flutter/test/widgets/draggable_scrollable_sheet_test.dart +++ b/packages/flutter/test/widgets/draggable_scrollable_sheet_test.dart @@ -1906,4 +1906,81 @@ void main() { ); expect(lastExtent, .25); }); + + testWidgets( + 'DraggableScrollableSheet with BouncingScrollPhysics snaps when bouncing over max extent', + (WidgetTester tester) async { + double? lastExtent; + + final DraggableScrollableController controller = DraggableScrollableController(); + addTearDown(controller.dispose); + + final List children = List.generate(12, (int index) { + return Padding( + padding: const EdgeInsets.only(bottom: 10), + child: Container(color: Colors.green, height: 100, child: Text('Item $index')), + ); + }); + children.insert(0, Container(color: Colors.green, height: 100)); + + await tester.pumpWidget( + MaterialApp( + home: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return Scaffold( + body: DraggableScrollableSheet( + initialChildSize: 0.25, + snap: true, + snapSizes: const [0.25, 0.5, 1.0], + controller: controller, + builder: (BuildContext context, ScrollController scrollController) { + return NotificationListener( + onNotification: (DraggableScrollableNotification notification) { + lastExtent = notification.extent; + return false; + }, + child: ColoredBox( + color: const Color(0xFFABCDEF), + child: CustomScrollView( + physics: const BouncingScrollPhysics(), + controller: scrollController, + slivers: [ + SliverList( + delegate: SliverChildBuilderDelegate((_, int index) { + return children[index]; + }, childCount: children.length), + ), + ], + ), + ), + ); + }, + ), + ); + }, + ), + ), + ); + + await tester.pumpAndSettle(); + final double itemHeight = tester.getSize(find.text('Item 0')).height; + + controller.jumpTo(1.0); + await tester.pumpAndSettle(); + + await tester.fling(find.text('Item 0'), Offset(0, -itemHeight), 100); + await tester.pumpFrames( + tester.widget(find.byType(MaterialApp)), + const Duration(milliseconds: 500), + ); + + await tester.fling(find.text('Item 2'), Offset(0, itemHeight), 500); + await tester.pumpFrames( + tester.widget(find.byType(MaterialApp)), + const Duration(milliseconds: 500), + ); + + expect(lastExtent, 1.0); + }, + ); }