Skip to content
This repository was archived by the owner on Nov 24, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/src/snapping_position.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:math';

import 'package:flutter/widgets.dart';

/// This class is an helper class for specifying the [grabbingContentOffset]
Expand Down Expand Up @@ -47,12 +49,16 @@ class SnappingPosition {
/// The snapping duration
final Duration snappingDuration;

/// Whether to respect max height when snapping to new position via pixels
final bool respectMaxHeight;

/// Creates a snapping position that is given by the amount of pixels
const SnappingPosition.pixels({
required double positionPixels,
this.snappingCurve = Curves.ease,
this.snappingDuration = const Duration(milliseconds: 250),
this.grabbingContentOffset = GrabbingContentOffset.middle,
this.respectMaxHeight = false,
}) : this._positionPixel = positionPixels,
this._positionFactor = null;

Expand All @@ -65,7 +71,8 @@ class SnappingPosition {
this.snappingDuration = const Duration(milliseconds: 250),
this.grabbingContentOffset = GrabbingContentOffset.middle,
}) : this._positionPixel = null,
this._positionFactor = positionFactor;
this._positionFactor = positionFactor,
this.respectMaxHeight = false;

double getPositionInPixels(double maxHeight, double grabbingHeight) {
var centerPosition = this._getCenterPositionInPixels(maxHeight);
Expand All @@ -74,7 +81,10 @@ class SnappingPosition {
}

double _getCenterPositionInPixels(double maxHeight) {
if (this._positionPixel != null) return this._positionPixel!;
if (this._positionPixel != null) {
if (respectMaxHeight) return min(maxHeight, this._positionPixel!);
return this._positionPixel!;
}
return this._positionFactor! * maxHeight;
}

Expand Down
8 changes: 8 additions & 0 deletions test/snapping_position_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ void main() {
);
expect(snappingPosition.getPositionInPixels(1000, 100), 1200);
});
test('Testing respect max height', () {
var snappingPosition = SnappingPosition.pixels(
positionPixels: 1200,
grabbingContentOffset: GrabbingContentOffset.middle,
respectMaxHeight: true,
);
expect(snappingPosition.getPositionInPixels(1000, 100), 1000);
});
test('Testing undershoot position', () {
var snappingPosition = SnappingPosition.pixels(
positionPixels: -1200,
Expand Down