scrollToIndex method

Future<void> scrollToIndex(
  1. int targetIndex
)

Implementation

Future<void> scrollToIndex(int targetIndex) async {
  if (targetIndex == -1) {
    return;
  }
  while (true) {
    final key = dataGroups.itemsKeys[targetIndex];
    if (key == null) {
      await Future.delayed(const Duration(milliseconds: 50));
      continue;
    }

    final context = key.currentContext;
    if (context != null && context.mounted) {
      await Scrollable.ensureVisible(
        context,
        duration: const Duration(milliseconds: 1),
        curve: Curves.easeInOut,
      );
      break;
    } else {
      await animateTo(
        offset + 400,
        duration: const Duration(milliseconds: 1),
        curve: Curves.linear,
      );
    }
    if (position.pixels >= position.maxScrollExtent) {
      break;
    }
  }
}