fix: ensure draggable_scrollable_sheet respects shouldCloseOnMinExten… (#156338)

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<void> 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'),
          ),
        ),
      );
}
```
This commit is contained in:
Aparin Kirill
2024-11-06 03:55:06 +03:00
committed by GitHub
parent 7edcacb4d5
commit 7a57b69fcf
2 changed files with 34 additions and 2 deletions

View File

@@ -623,8 +623,8 @@ class _DraggableSheetExtent {
required bool snap,
required List<double> 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<DraggableScrollableSheet> {
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.

View File

@@ -1746,4 +1746,35 @@ void main() {
areCreateAndDispose,
);
});
testWidgets('DraggableScrollableSheet respects shouldCloseOnMinExtent', (WidgetTester tester) async {
final DraggableScrollableController controller = DraggableScrollableController();
DraggableScrollableNotification? receivedNotification;
Future<void> 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();
});
}