Jaime Blasco
Committed by GitHub

Merge pull request #8 from jamesblasco/scroll-end-velocity

Add support to close a modal with a scroll view by dragging fast
... ... @@ -178,7 +178,6 @@ class _ModalBottomSheetState extends State<ModalBottomSheet>
final canClose = await shouldClose();
if (canClose) {
_close();
print('close');
return;
} else {
_cancelClose();
... ... @@ -213,13 +212,11 @@ class _ModalBottomSheetState extends State<ModalBottomSheet>
// If speed is bigger than _minFlingVelocity try to close it
if (velocity > _minFlingVelocity) {
_close();
print('close2');
} else if (hasReachedCloseThreshold) {
if (widget.animationController.value > 0.0) {
widget.animationController.fling(velocity: -1.0);
}
_close();
print('close3');
} else {
_cancelClose();
}
... ... @@ -228,6 +225,14 @@ class _ModalBottomSheetState extends State<ModalBottomSheet>
}
}
// As we cannot access the dragGesture detector of the scroll view
// we can not know the DragDownDetails and therefore the end velocity.
// VelocityTracker it is used to calculate the end velocity of the scroll
// when user is trying to close the modal by dragging
VelocityTracker _velocityTracker;
DateTime _startTime;
void _handleScrollUpdate(ScrollNotification notification) {
if (notification.metrics.pixels <= notification.metrics.minScrollExtent) {
//Check if listener is same from scrollController
... ... @@ -237,6 +242,10 @@ class _ModalBottomSheetState extends State<ModalBottomSheet>
return;
}
DragUpdateDetails dragDetails;
if (notification is ScrollStartNotification) {
_velocityTracker = VelocityTracker();
_startTime = DateTime.now();
}
if (notification is ScrollUpdateNotification) {
dragDetails = notification.dragDetails;
}
... ... @@ -244,12 +253,15 @@ class _ModalBottomSheetState extends State<ModalBottomSheet>
dragDetails = notification.dragDetails;
}
if (dragDetails != null) {
print('scroll');
final duration = _startTime.difference(DateTime.now());
final offset = Offset(0, _scrollController.offset);
_velocityTracker.addPosition(duration, offset);
_handleDragUpdate(dragDetails.primaryDelta);
}
// Todo: detect dragEnd during scroll so it can bottom sheet can close
// if velocity > _minFlingVelocity
else if (isDragging) _handleDragEnd(0);
else if (isDragging) {
final velocity = _velocityTracker.getVelocity().pixelsPerSecond.dy;
_handleDragEnd(velocity);
}
}
}
... ...