animateToIndex method
Future<bool>
animateToIndex(
- int index, {
- Duration duration = const Duration(milliseconds: 300),
- Curve curve = Curves.easeInOut,
- double alignment = 0.0,
- EdgeInsets padding = EdgeInsets.zero,
- BuildContext? sliverContext,
- bool isFixedHeight = false,
Animates to the item at the given index with smooth scrolling.
The duration defaults to 300ms. The curve defaults to Curves.easeInOut.
The alignment determines where the item should be positioned in the viewport
(0.0 = top/left, 0.5 = center, 1.0 = bottom/right).
Returns a Future that completes when the animation finishes.
Returns false if no observer controller is attached or the index is out of bounds.
Example:
await cubit.animateToIndex(10);
await cubit.animateToIndex(5, duration: Duration(milliseconds: 500));
await cubit.animateToIndex(0, alignment: 0.5); // Center the item
Implementation
Future<bool> animateToIndex(
int index, {
Duration duration = const Duration(milliseconds: 300),
Curve curve = Curves.easeInOut,
double alignment = 0.0,
EdgeInsets padding = EdgeInsets.zero,
BuildContext? sliverContext,
bool isFixedHeight = false,
}) async {
// Validate index
final items = currentItems;
if (index < 0 || index >= items.length) {
_logger.w('animateToIndex: index $index out of bounds (0-${items.length - 1})');
return false;
}
try {
if (_listObserverController != null) {
await _listObserverController!.animateTo(
index: index,
duration: duration,
curve: curve,
alignment: alignment,
padding: padding,
sliverContext: sliverContext,
isFixedHeight: isFixedHeight,
);
_logger.d('Animated to index $index');
return true;
}
if (_gridObserverController != null) {
await _gridObserverController!.animateTo(
index: index,
duration: duration,
curve: curve,
alignment: alignment,
padding: padding,
sliverContext: sliverContext,
isFixedHeight: isFixedHeight,
);
_logger.d('Animated to index $index (grid)');
return true;
}
_logger.w('animateToIndex: no observer controller attached');
return false;
} catch (e, stackTrace) {
_logger.e('animateToIndex failed', error: e, stackTrace: stackTrace);
return false;
}
}