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

feat: animation curve and duration can depend on estimated distance #10

Merged
merged 1 commit into from
Feb 17, 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/examples/item_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,8 @@ class _ItemListPageState extends ExamplePageState {
index: item,
scrollController: _scrollController,
alignment: alignment,
duration: const Duration(milliseconds: 300),
curve: Curves.easeOutCubic,
duration: (_) => const Duration(milliseconds: 300),
curve: (_) => Curves.easeOutCubic,
);
},
);
Expand Down
4 changes: 2 additions & 2 deletions example/lib/examples/long_document.dart
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ class _LogDocumentPageState extends ExamplePageState<LongDocumentPage> {
index: item,
scrollController: _scrollController,
alignment: alignment,
duration: const Duration(milliseconds: 300),
curve: Curves.easeOutCubic,
duration: (_) => const Duration(milliseconds: 300),
curve: (_) => Curves.easeOutCubic,
);
},
);
Expand Down
20 changes: 15 additions & 5 deletions lib/src/animate_to_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,28 @@ class AnimateToItem {
final double alignment;
final Rect? rect;
final ScrollPosition position;
final Duration duration;
final Curve curve;
final Duration Function(double estimatedDistance) duration;
final Curve Function(double estimatedDistance) curve;

double lastPosition = 0.0;

void animate() {
final start = position.pixels;
final estimatedTarget = extentManager.getOffsetToReveal(
index,
alignment,
rect: rect,
estimationOnly: true,
);
final estimatedDistance = (estimatedTarget - start).abs();
final controller = AnimationController(
vsync: position.context.vsync,
duration: duration,
duration: duration(estimatedDistance),
);
final animation = CurvedAnimation(
parent: controller,
curve: curve,
curve: curve(estimatedDistance),
);
final start = position.pixels;
animation.addListener(() {
final value = animation.value;
final targetPosition = extentManager.getOffsetToReveal(
Expand All @@ -42,6 +51,7 @@ class AnimateToItem {
estimationOnly: value < 1.0,
);
final jumpPosition = lerpDouble(start, targetPosition, value)!;
lastPosition = jumpPosition;
position.jumpTo(jumpPosition);
});
controller.forward();
Expand Down
4 changes: 2 additions & 2 deletions lib/src/super_sliver_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class ExtentController extends ChangeNotifier {
required int index,
required ScrollController scrollController,
required double alignment,
required Duration duration,
required Curve curve,
required Duration Function(double estimatedDistance) duration,
required Curve Function(double estimatedDistance) curve,
Rect? rect,
}) {
assert(_delegate != null, "ExtentController is not attached.");
Expand Down