Repository: fujidaiti/smooth_sheets
Pull request #252 (2024-09-27 14:02:11)

Refine snapping physics
## Description

- Rename `snapTo` to `anchors`
- Rename `snappingBehavior` to `behavior`

## Summary (check all that apply)

- [x] Modified / added code
- [x] Modified / added tests
- [x] Modified / added examples
- [ ] Modified / added others (pubspec.yaml, workflows, etc...)
- [ ] Updated README
- [x] Contains breaking changes
  - [x] Created / updated migration guide
- [ ] Incremented version number
  - [ ] Updated CHANGELOG



Patch:
diff --git a/example/lib/showcase/airbnb_mobile_app.dart b/example/lib/showcase/airbnb_mobile_app.dart
index 3543204..c31d048 100644
--- a/example/lib/showcase/airbnb_mobile_app.dart
+++ b/example/lib/showcase/airbnb_mobile_app.dart
@@ -166,8 +166,8 @@ class _ContentSheet extends StatelessWidget {
 
         final sheetPhysics = BouncingSheetPhysics(
           parent: SnappingSheetPhysics(
-            snappingBehavior: SnapToNearest(
-              snapTo: [
+            behavior: SnapToNearest(
+              anchors: [
                 minSheetPosition,
                 const SheetAnchor.proportional(1),
               ],
diff --git a/example/lib/tutorial/bottom_bar_visibility.dart b/example/lib/tutorial/bottom_bar_visibility.dart
index f4e3bb7..be8fd60 100644
--- a/example/lib/tutorial/bottom_bar_visibility.dart
+++ b/example/lib/tutorial/bottom_bar_visibility.dart
@@ -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],
         ),
       ),
     );
diff --git a/example/lib/tutorial/extent_driven_animation.dart b/example/lib/tutorial/extent_driven_animation.dart
index cac4d35..6e71884 100644
--- a/example/lib/tutorial/extent_driven_animation.dart
+++ b/example/lib/tutorial/extent_driven_animation.dart
@@ -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)],
         ),
       ),
     );
diff --git a/example/lib/tutorial/sheet_content_scaffold.dart b/example/lib/tutorial/sheet_content_scaffold.dart
index e082175..1e8f55a 100644
--- a/example/lib/tutorial/sheet_content_scaffold.dart
+++ b/example/lib/tutorial/sheet_content_scaffold.dart
@@ -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),
diff --git a/example/lib/tutorial/sheet_physics.dart b/example/lib/tutorial/sheet_physics.dart
index f99284b..9965fef 100644
--- a/example/lib/tutorial/sheet_physics.dart
+++ b/example/lib/tutorial/sheet_physics.dart
@@ -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),
         ],
diff --git a/example/lib/tutorial/textfield_with_multiple_stops.dart b/example/lib/tutorial/textfield_with_multiple_stops.dart
index 762a1e0..08bd265 100644
--- a/example/lib/tutorial/textfield_with_multiple_stops.dart
+++ b/example/lib/tutorial/textfield_with_multiple_stops.dart
@@ -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),
diff --git a/lib/src/foundation/sheet_physics.dart b/lib/src/foundation/sheet_physics.dart
index 415a9e3..0b04a51 100644
--- a/lib/src/foundation/sheet_physics.dart
+++ b/lib/src/foundation/sheet_physics.dart
@@ -227,12 +227,11 @@ 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.
@@ -240,12 +239,12 @@ class SnapToNearest implements SnappingSheetBehavior {
 
   @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;
 
@@ -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;
@@ -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 &&
@@ -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);
   }
 }
diff --git a/migrations/migration-guide-0.10.x.md b/migrations/migration-guide-0.10.x.md
index 71364fb..c7ae846 100644
--- a/migrations/migration-guide-0.10.x.md
+++ b/migrations/migration-guide-0.10.x.md
@@ -11,15 +11,18 @@ 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:
 
@@ -27,7 +30,7 @@ and is not a core feature of the package.
 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.


Test patch:
diff --git a/test/foundation/physics_test.dart b/test/foundation/physics_test.dart
index 9939e7e..1bb1f4a 100644
--- a/test/foundation/physics_test.dart
+++ b/test/foundation/physics_test.dart
@@ -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),