From 7a57b69fcf959e4f471dc05318b7cc2d352400f4 Mon Sep 17 00:00:00 2001 From: Aparin Kirill <41366638+kirya355@users.noreply.github.com> Date: Wed, 6 Nov 2024 03:55:06 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20ensure=20draggable=5Fscrollable=5Fsheet?= =?UTF-8?q?=20respects=20shouldCloseOnMinExten=E2=80=A6=20(#156338)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR fixes the `shouldCloseOnMinExtent` flag in `draggable_scrollable_sheet.dart`. *The `shouldCloseOnMinExtent` flag was not functioning correctly in the `DraggableScrollableSheet` widget. This PR ensures that the flag is properly handled, preventing the sheet from closing when it reaches the minimum extent if the flag is set to false. Before/after screenshots are not applicable for this change.* In the code sample below, before my fix, the sheet would close when scrolled down. After my fix, it behaves as expected by respecting the `shouldCloseOnMinExtent` parameter. ### Code Sample ```dart import 'package:flutter/material.dart'; Future main() async { runApp(const MaterialApp( home: Home(), )); } class Home extends StatelessWidget { const Home({ super.key, }); @override Widget build(BuildContext context) => Scaffold( body: Center( child: FilledButton( onPressed: () async => showModalBottomSheet( context: context, isScrollControlled: true, isDismissible: false, builder: (context) => DraggableScrollableSheet( expand: false, maxChildSize: 0.9, minChildSize: 0.25, initialChildSize: 0.5, shouldCloseOnMinExtent: false, builder: (context, scrollController) => Navigator( onGenerateRoute: (settings) => MaterialPageRoute( settings: settings, builder: (context) => ListView.builder( controller: scrollController, itemExtent: 100, itemCount: 100, itemBuilder: (context, index) => Center( child: Text('$index'), ), ), ), ), ), ), child: const Text('Open sheet'), ), ), ); } ``` --- .../widgets/draggable_scrollable_sheet.dart | 5 +-- .../draggable_scrollable_sheet_test.dart | 31 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart b/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart index 38e1bcc424..b8e4e16d8a 100644 --- a/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart +++ b/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart @@ -623,8 +623,8 @@ class _DraggableSheetExtent { required bool snap, required List snapSizes, required double initialSize, - Duration? snapAnimationDuration, - bool shouldCloseOnMinExtent = true, + required Duration? snapAnimationDuration, + required bool shouldCloseOnMinExtent, }) { return _DraggableSheetExtent( minSize: minSize, @@ -744,6 +744,7 @@ class _DraggableScrollableSheetState extends State { snapSizes: _impliedSnapSizes(), snapAnimationDuration: widget.snapAnimationDuration, initialSize: widget.initialChildSize, + shouldCloseOnMinExtent: widget.shouldCloseOnMinExtent, ); // Modify the existing scroll controller instead of replacing it so that // developers listening to the controller do not have to rebuild their listeners. diff --git a/packages/flutter/test/widgets/draggable_scrollable_sheet_test.dart b/packages/flutter/test/widgets/draggable_scrollable_sheet_test.dart index 1b5fc12749..9bf10a0de9 100644 --- a/packages/flutter/test/widgets/draggable_scrollable_sheet_test.dart +++ b/packages/flutter/test/widgets/draggable_scrollable_sheet_test.dart @@ -1746,4 +1746,35 @@ void main() { areCreateAndDispose, ); }); + + testWidgets('DraggableScrollableSheet respects shouldCloseOnMinExtent', (WidgetTester tester) async { + final DraggableScrollableController controller = DraggableScrollableController(); + DraggableScrollableNotification? receivedNotification; + + Future pumpWidgetAndFling() async { + await tester.pumpWidget(boilerplateWidget( + null, + controller: controller, + shouldCloseOnMinExtent: false, + onDraggableScrollableNotification: (DraggableScrollableNotification notification) { + receivedNotification = notification; + return false; + }, + )); + + await tester.flingFrom(const Offset(0, 325), const Offset(0, -325), 200); + await tester.pumpAndSettle(); + } + + await pumpWidgetAndFling(); + expect(receivedNotification!.shouldCloseOnMinExtent, isFalse); + + receivedNotification = null; + controller.jumpTo(0.5); + + // Construct the widget a second time, to ensure didUpdateWidget is called. + await pumpWidgetAndFling(); + expect(receivedNotification!.shouldCloseOnMinExtent, isFalse); + controller.dispose(); + }); }