Skip to content

Commit

Permalink
feat: returning null for item getter during animateToItem will stop t…
Browse files Browse the repository at this point in the history
…he animation (#63)
  • Loading branch information
knopp authored Jun 10, 2024
1 parent b5bd4b2 commit b84a425
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
21 changes: 18 additions & 3 deletions lib/src/animate_to_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class AnimateToItem {
});

final ExtentManager extentManager;
final ValueGetter<int> index;
final ValueGetter<int?> index;
final double alignment;
final Rect? rect;
final ScrollPosition position;
Expand All @@ -26,9 +26,13 @@ class AnimateToItem {
double lastPosition = 0.0;

void animate() {
final index = this.index();
if (index == null) {
return;
}
final start = position.pixels;
final estimatedTarget = extentManager.getOffsetToReveal(
index(),
index,
alignment,
rect: rect,
estimationOnly: true,
Expand All @@ -38,14 +42,25 @@ class AnimateToItem {
vsync: position.context.vsync,
duration: duration(estimatedDistance),
);
controller.addStatusListener((status) {
if (status == AnimationStatus.completed) {
controller.dispose();
}
});
final animation = CurvedAnimation(
parent: controller,
curve: curve(estimatedDistance),
);
animation.addListener(() {
final value = animation.value;
final index = this.index();
if (index == null) {
controller.stop();
controller.dispose();
return;
}
var targetPosition = extentManager.getOffsetToReveal(
index(),
index,
alignment,
rect: rect,
estimationOnly: value < 1.0,
Expand Down
5 changes: 3 additions & 2 deletions lib/src/super_sliver_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ class ListController extends ChangeNotifier {
///
/// The index getter will be called repeatedly on every animation tick, which
/// allows for accommodating index changes when items are inserted or removed
/// during the animation.
/// during the animation. Returning `null` from the index getter will stop
/// the animation.
///
/// The optional [rect] parameter describes which area of that target item
/// should be revealed in the viewport. If omitted, the entire item
Expand All @@ -124,7 +125,7 @@ class ListController extends ChangeNotifier {
/// the middle of the viewport. If the value is 1.0, the item will be
/// positioned at the trailing edge of the viewport.
void animateToItem({
required ValueGetter<int> index,
required ValueGetter<int?> index,
required ScrollController scrollController,
required double alignment,
required Duration Function(double estimatedDistance) duration,
Expand Down

0 comments on commit b84a425

Please sign in to comment.