Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine snapping physics #252

Merged
merged 3 commits into from
Sep 27, 2024
Merged
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
4 changes: 2 additions & 2 deletions example/lib/showcase/airbnb_mobile_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ class _ContentSheet extends StatelessWidget {

final sheetPhysics = BouncingSheetPhysics(
parent: SnappingSheetPhysics(
snappingBehavior: SnapToNearest(
snapTo: [
behavior: SnapToNearest(
anchors: [
minSheetPosition,
const SheetAnchor.proportional(1),
],
Expand Down
4 changes: 2 additions & 2 deletions example/lib/tutorial/bottom_bar_visibility.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ class _ExampleSheet extends StatelessWidget {

const multiStopPhysics = BouncingSheetPhysics(
parent: SnappingSheetPhysics(
snappingBehavior: SnapToNearest(
snapTo: [minSize, halfSize, fullSize],
behavior: SnapToNearest(
anchors: [minSize, halfSize, fullSize],
),
),
);
Expand Down
4 changes: 2 additions & 2 deletions example/lib/tutorial/extent_driven_animation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class _ExampleSheet extends StatelessWidget {

final physics = BouncingSheetPhysics(
parent: SnappingSheetPhysics(
snappingBehavior: SnapToNearest(
snapTo: [minPosition, const SheetAnchor.proportional(1)],
behavior: SnapToNearest(
anchors: [minPosition, const SheetAnchor.proportional(1)],
),
),
);
Expand Down
4 changes: 2 additions & 2 deletions example/lib/tutorial/sheet_content_scaffold.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ class _ExampleSheet extends StatelessWidget {

const physics = BouncingSheetPhysics(
parent: SnappingSheetPhysics(
snappingBehavior: SnapToNearest(
snapTo: [
behavior: SnapToNearest(
anchors: [
SheetAnchor.proportional(0.2),
SheetAnchor.proportional(0.5),
SheetAnchor.proportional(1),
Expand Down
4 changes: 2 additions & 2 deletions example/lib/tutorial/sheet_physics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ class _MySheet extends StatelessWidget {
// - the position at which the entire content is visible.
// Note that the "position" is the visible height of the sheet.
const snappingPhysics = SnappingSheetPhysics(
snappingBehavior: SnapToNearest(
snapTo: [
behavior: SnapToNearest(
anchors: [
SheetAnchor.proportional(_halfwayFraction),
SheetAnchor.proportional(1),
],
Expand Down
4 changes: 2 additions & 2 deletions example/lib/tutorial/textfield_with_multiple_stops.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class TextFieldWithMultipleStops extends StatelessWidget {
minPosition: const SheetAnchor.proportional(0.4),
physics: const BouncingSheetPhysics(
parent: SnappingSheetPhysics(
snappingBehavior: SnapToNearest(
snapTo: [
behavior: SnapToNearest(
anchors: [
SheetAnchor.proportional(0.4),
SheetAnchor.proportional(0.7),
SheetAnchor.proportional(1),
Expand Down
21 changes: 10 additions & 11 deletions lib/src/foundation/sheet_physics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -227,25 +227,24 @@ class SnapToNearestEdge implements SnappingSheetBehavior {

class SnapToNearest implements SnappingSheetBehavior {
const SnapToNearest({
required this.snapTo,
required this.anchors,
this.minFlingSpeed = kMinFlingVelocity,
}) : assert(minFlingSpeed >= 0);

// TODO: Rename to `detents`.
final List<SheetAnchor> snapTo;
final List<SheetAnchor> anchors;

/// The lowest speed (in logical pixels per second)
/// at which a gesture is considered to be a fling.
final double minFlingSpeed;

@override
SheetAnchor? findSettledPosition(double velocity, SheetMetrics metrics) {
if (snapTo.length <= 1) {
return snapTo.firstOrNull;
if (anchors.length <= 1) {
return anchors.firstOrNull;
}

final (sortedDetents, nearestIndex) = sortPositionsAndFindNearest(
snapTo, metrics.pixels, metrics.contentSize);
anchors, metrics.pixels, metrics.contentSize);
final cmp = FloatComp.distance(metrics.devicePixelRatio);
final pixels = metrics.pixels;

Expand Down Expand Up @@ -396,10 +395,10 @@ class SnappingSheetPhysics extends SheetPhysics with SheetPhysicsMixin {
const SnappingSheetPhysics({
super.parent,
this.spring = kDefaultSheetSpring,
this.snappingBehavior = const SnapToNearestEdge(),
this.behavior = const SnapToNearestEdge(),
});

final SnappingSheetBehavior snappingBehavior;
final SnappingSheetBehavior behavior;

@override
final SpringDescription spring;
Expand All @@ -413,13 +412,13 @@ class SnappingSheetPhysics extends SheetPhysics with SheetPhysicsMixin {
return SnappingSheetPhysics(
parent: parent ?? this.parent,
spring: spring ?? this.spring,
snappingBehavior: snappingBehavior ?? this.snappingBehavior,
behavior: snappingBehavior ?? this.behavior,
);
}

@override
Simulation? createBallisticSimulation(double velocity, SheetMetrics metrics) {
final detent = snappingBehavior
final detent = behavior
.findSettledPosition(velocity, metrics)
?.resolve(metrics.contentSize);
if (detent != null &&
Expand All @@ -438,7 +437,7 @@ class SnappingSheetPhysics extends SheetPhysics with SheetPhysicsMixin {

@override
SheetAnchor findSettledPosition(double velocity, SheetMetrics metrics) {
return snappingBehavior.findSettledPosition(velocity, metrics) ??
return behavior.findSettledPosition(velocity, metrics) ??
super.findSettledPosition(velocity, metrics);
}
}
Expand Down
17 changes: 10 additions & 7 deletions migrations/migration-guide-0.10.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,26 @@ The changes in v0.10.0 are summarized as follows. Breaking changes are marked wi
- `SheetMetrics` is now a mixin and can no longer be instantiated directly. Use
the `SheetMetricsSnapshot` class for this purpose.

## Change in `SnappingSheetBehavior` :boom:
## Change in `SnappingSheetBehavior` and its subclasses :boom:

The `findSnapPixels` method has been removed. Use `findSettledPosition` instead.
- The `findSnapPixels` method has been removed. Use `findSettledPosition` instead.
- `SnapToNearest.snapTo` has been renamed to `anchors`.

## Changes in `SheetPhysics` :boom:
## Changes in `SheetPhysics` and related classes :boom:

The `createSettlingSimulation` method has been removed in favor of the `findSettledPosition` method.
As a result, `InterpolationSimulation` has also been removed since it is no longer used internally
and is not a core feature of the package.
- The `createSettlingSimulation` method has been removed in favor of the `findSettledPosition`
method.
- As a result, `InterpolationSimulation` has also been removed since it is no longer used internally
and is not a core feature of the package.
- `SnappingSheetPhysics.snappingBehavior` has been renamed to `behavior`.

## Changes in `SheetController` :boom:

`SheetController` is no longer a notifier of `SheetMetrics`, and is now a notifier of the sheet
position (`double?`) instead. It is still possible to access the `SheetMetrics` object through
the `SheetController.metrics` getter.

## Changes in `Extent` :boom:
## Changes in `Extent` and its subclasses :boom:

`Extent`, `FixedExtent`, and `ProportionalExtent` have been renamed
to `SheetAnchor`, `FixedSheetAnchor`, and `ProportionalSheetAnchor`, respectively.
2 changes: 1 addition & 1 deletion test/foundation/physics_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ void main() {
setUp(() {
behaviorUnderTest = SnapToNearest(
minFlingSpeed: 50,
snapTo: [
anchors: [
SheetAnchor.pixels(_positionAtBottomEdge.pixels),
SheetAnchor.pixels(_positionAtMiddle.pixels),
SheetAnchor.pixels(_positionAtTopEdge.pixels),
Expand Down
Loading