From cd0082f04f37f2c7e1ea38ac18c235a2184492fa Mon Sep 17 00:00:00 2001
From: Chikamatsu Kazuya <43089218+chika3742@users.noreply.github.com>
Date: Tue, 8 Apr 2025 13:23:34 +0900
Subject: [PATCH] Fix: DraggableScrollableSheet may not close if snapping is
enabled (#165557)
fixes #140701
This PR fixes an issue where a DraggableScrollableSheet with `snap` set
to true and `shouldCloseOnMinExtent` set to true may not close when
dragged downward.
The issue was caused by round-off errors accumulated by `addPixelDelta`
method, which could lead to `extent.currentSize` not matching the
`extent.minSize` exactly when the bottom is reached. I added logic to
correct it when the snapping ballistic animation is complete.
Sample code
```Dart
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("DraggableScrollableSheet Test"),
),
body: Center(
child: ElevatedButton(
child: Text("Open"),
onPressed: () {
showModalBottomSheet(
context: context,
showDragHandle: true,
isScrollControlled: true,
builder: (context) => _buildBottomSheet(),
);
},
),
),
);
}
Widget _buildBottomSheet() {
return NotificationListener(
onNotification: (notification) {
print(notification.extent);
return false;
},
child: DraggableScrollableSheet(
expand: false,
snap: true,
shouldCloseOnMinExtent: true,
maxChildSize: 0.9,
minChildSize: 0.25,
builder: (context, scrollController) {
return ListView.builder(
controller: scrollController,
itemCount: 100,
itemBuilder: (context, index) {
return ListTile(
title: Text("Item $index"),
);
},
);
},
),
);
}
}
```
| Before applying fix | After |
| --- | --- |
| * Occurs with probability