Skip to content

Commit

Permalink
Fixed issue with closeProgressThreshold for smaller bottom sheets
Browse files Browse the repository at this point in the history
When a ModalBottomSheets that due to its content is smaller than a full screen bottom sheet, the closeProgressThreshold is calculated based on how far the dialog has to move based on the percentage of the viewport/fullscreen dialog.

We need to adjust for this based on the height difference of the viewport and the rendered content within the bottom sheet dialog

Related issue: #jamesblasco#421
  • Loading branch information
Bjarte Bore committed Nov 27, 2024
1 parent a87f82b commit 6ea14aa
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions modal_bottom_sheet/lib/src/bottom_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class ModalBottomSheet extends StatefulWidget {
class ModalBottomSheetState extends State<ModalBottomSheet>
with TickerProviderStateMixin {
final GlobalKey _childKey = GlobalKey(debugLabel: 'BottomSheet child');
final GlobalKey _contentKey = GlobalKey(debugLabel: 'BottomSheet content');

ScrollController get _scrollController => widget.scrollController;

Expand All @@ -149,6 +150,13 @@ class ModalBottomSheetState extends State<ModalBottomSheet>
return renderBox.size.height;
}


double? get _contentHeight {
final childContext = _contentKey.currentContext;
final renderBox = childContext?.findRenderObject() as RenderBox;
return renderBox.size.height;
}

bool get _dismissUnderway =>
widget.animationController.status == AnimationStatus.reverse;

Expand All @@ -160,8 +168,31 @@ class ModalBottomSheetState extends State<ModalBottomSheet>
bool get hasReachedWillPopThreshold =>
widget.animationController.value < _willPopThreshold;

bool get hasReachedCloseThreshold =>
widget.animationController.value < widget.closeProgressThreshold;
bool get hasReachedCloseThreshold {
final childHeight = _childHeight;
final contentHeight = _contentHeight;

if (contentHeight == null || childHeight == null || childHeight <= contentHeight) {
return widget.animationController.value < widget.closeProgressThreshold;
}

// When the content view is smaller that the child view
// we need to change the animation value to account for
// the height difference between the viewport and the content
final closeProgressThreshold = widget.closeProgressThreshold;

// Interpolate the value intop between 1 and the lower bound
final value = (widget.animationController.value - _lowerBound) / (1 - _lowerBound);

return value < closeProgressThreshold;
}

double get _lowerBound {
if (_contentHeight == null || _childHeight == null) {
return 1;
}
return (_contentHeight! / _childHeight!).clamp(0.0, 1.0);
}

void _close() {
isDragging = false;
Expand Down Expand Up @@ -392,7 +423,10 @@ class ModalBottomSheetState extends State<ModalBottomSheet>
_handleScrollUpdate(notification);
return false;
},
child: child!,
child: KeyedSubtree(
key: _contentKey,
child: child!,
),
),
),
),
Expand Down

0 comments on commit 6ea14aa

Please sign in to comment.