I’m working on a Flutter app and I’m trying to implement automatic scrolling to a specific index in a ListView after the widget tree has been built. I’m using the following code to achieve this:
dart
WidgetsBinding.instance.addPostFrameCallback((_) {
_animateToIndex(10); // Index of the item I want to scroll to
});
Here is the _animateToIndex function:
void _animateToIndex(int index) {
_scrollController.animateTo(
index * _itemHeight, // Assuming each item has the same height
duration: Duration(seconds: 1),
curve: Curves.easeInOut,
);
}
This works perfectly on iOS, but on Android, the scroll does not happen. I’ve confirmed that the _animateToIndex method is being called on both platforms.