From 77ac8dbbb0fb25ca2537e2f3d2a83f2450de1ac4 Mon Sep 17 00:00:00 2001 From: emakar Date: Wed, 22 Oct 2025 02:41:14 +0300 Subject: [PATCH] don't break sheet's snap from physics (#171157) Fixes https://github.com/flutter/flutter/issues/169712 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. --- .../widgets/draggable_scrollable_sheet.dart | 4 + .../draggable_scrollable_sheet_test.dart | 77 +++++++++++++++++++ 2 files changed, 81 insertions(+) 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); + }, + ); }