Skip to content

Commit

Permalink
Merge pull request #4 from tappeddev/fix-jumping-on-min-max-height-ch…
Browse files Browse the repository at this point in the history
…ange

fix-jumping-on-min-max-height-change
  • Loading branch information
JulianBissekkou authored Nov 11, 2024
2 parents 9f0fdda + 076972a commit 35b198b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:

# Cancel previous builds by only allowing one concurrent build per ref.
concurrency:
group: continuous_ops_check-${{ github.ref }}
group: tapped-bottom-sheet-${{ github.ref }}
cancel-in-progress: true


Expand All @@ -19,8 +19,8 @@ jobs:
timeout-minutes: 5
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: kuhnroyal/flutter-fvm-config-action@v1
- uses: actions/checkout@v4
- uses: kuhnroyal/flutter-fvm-config-action@v2
with:
path: '.fvm/fvm_config.json'
- uses: subosito/flutter-action@v2
Expand All @@ -40,8 +40,8 @@ jobs:
timeout-minutes: 5
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: kuhnroyal/flutter-fvm-config-action@v1
- uses: actions/checkout@v4
- uses: kuhnroyal/flutter-fvm-config-action@v2
with:
path: '.fvm/fvm_config.json'
- uses: subosito/flutter-action@v2
Expand Down
29 changes: 29 additions & 0 deletions lib/scrollable_bottom_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ class ScrollableBottomSheet extends StatefulWidget {
final Duration animationDuration;
final double? initialPosition;

/// If set to [true] the sheet tries to maintain the position in pixels
/// when [maxHeight] or [minHeight] changes.
/// This avoids jumping since changes in any of the two values has an
/// direct impact on the sheets position.
/// Defaults to [false].
final bool maintainPositionOnConstraintChange;

final double borderRadiusTop;

final Color borderColor;
Expand Down Expand Up @@ -50,6 +57,7 @@ class ScrollableBottomSheet extends StatefulWidget {
this.shadows,
this.minFlingVelocity = _kMinFlingVelocity,
this.completeFlingVelocity = _kCompleteFlingVelocity,
this.maintainPositionOnConstraintChange = false,
});

@override
Expand All @@ -66,6 +74,7 @@ class ScrollableBottomSheetState extends State<ScrollableBottomSheet>
late final AnimationController _animationController;
var _isScrollingEnabled = false;
var _isScrollingBlocked = false;
var _didStartScrolling = false;

Drag? _drag;

Expand Down Expand Up @@ -100,6 +109,22 @@ class ScrollableBottomSheetState extends State<ScrollableBottomSheet>
super.dispose();
}

@override
void didUpdateWidget(covariant ScrollableBottomSheet oldWidget) {
super.didUpdateWidget(oldWidget);

if (!widget.maintainPositionOnConstraintChange) return;
if (oldWidget.maxHeight == widget.maxHeight &&
oldWidget.minHeight == widget.minHeight) return;

final previousPositionPixels = Tween(
begin: oldWidget.minHeight,
end: oldWidget.maxHeight,
).transform(_animationController.value);
final newValue = _pixelToValue(previousPositionPixels);
_animationController.value = newValue.clamp(0.0, 1.0);
}

@override
Widget build(BuildContext context) {
final borderRadius =
Expand Down Expand Up @@ -153,6 +178,7 @@ class ScrollableBottomSheetState extends State<ScrollableBottomSheet>
void _onDragUpdate(DragUpdateDetails details) {
final delta = details.delta;
final primaryDelta = delta.dy;
_didStartScrolling = true;

if (_isScrollingEnabled && _isPanelOpen) {
// _drag might be null if the drag activity ended and called _disposeDrag.
Expand Down Expand Up @@ -212,8 +238,11 @@ class ScrollableBottomSheetState extends State<ScrollableBottomSheet>
}

void _onDragEnd(DragEndDetails details) {
if (!_didStartScrolling) return;
if (_isScrollingBlocked) return;

_didStartScrolling = false;

// let the current animation finish before starting a new one
if (_animationController.isAnimating) return;

Expand Down

0 comments on commit 35b198b

Please sign in to comment.