diff --git a/lib/src/animate_to_item.dart b/lib/src/animate_to_item.dart index 2c9bcb5..a69e6ba 100644 --- a/lib/src/animate_to_item.dart +++ b/lib/src/animate_to_item.dart @@ -1,3 +1,4 @@ +import "dart:async"; import "dart:ui"; import "package:flutter/widgets.dart"; @@ -13,7 +14,7 @@ class AnimateToItem { required this.position, required this.duration, required this.curve, - }); + }) : _completer = Completer(); final ExtentManager extentManager; final ValueGetter index; @@ -22,13 +23,15 @@ class AnimateToItem { final ScrollPosition position; final Duration Function(double estimatedDistance) duration; final Curve Function(double estimatedDistance) curve; + + final Completer _completer; double lastPosition = 0.0; - void animate() { + Future animate() { final index = this.index(); if (index == null) { - return; + return Future.value(); } final start = position.pixels; final estimatedTarget = extentManager.getOffsetToReveal( @@ -44,6 +47,7 @@ class AnimateToItem { ); controller.addStatusListener((status) { if (status == AnimationStatus.completed) { + _completer.complete(); controller.dispose(); } }); @@ -56,6 +60,7 @@ class AnimateToItem { final index = this.index(); if (index == null) { controller.stop(); + _completer.complete(); controller.dispose(); return; } @@ -84,5 +89,6 @@ class AnimateToItem { position.jumpTo(jumpPosition); }); controller.forward(); + return _completer.future; } } diff --git a/lib/src/super_sliver_list.dart b/lib/src/super_sliver_list.dart index 84f9378..a071011 100644 --- a/lib/src/super_sliver_list.dart +++ b/lib/src/super_sliver_list.dart @@ -124,7 +124,7 @@ class ListController extends ChangeNotifier { /// edge of the viewport. If the value is 0.5, the item will be positioned in /// 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({ + Future animateToItem({ required ValueGetter index, required ScrollController scrollController, required double alignment, @@ -133,17 +133,18 @@ class ListController extends ChangeNotifier { Rect? rect, }) { assert(_delegate != null, "ListController is not attached."); - for (final position in scrollController.positions) { - AnimateToItem( - extentManager: _delegate!, - index: index, - alignment: alignment, - rect: rect, - position: position, - curve: curve, - duration: duration, - ).animate(); - } + return Future.wait([ + for (final position in scrollController.positions) + AnimateToItem( + extentManager: _delegate!, + index: index, + alignment: alignment, + rect: rect, + position: position, + curve: curve, + duration: duration, + ).animate() + ]); } /// Returns the range of items indices currently visible in the viewport.