jumpToIndex method

Future<void> jumpToIndex(
  1. int index, {
  2. Duration duration = const Duration(milliseconds: 20),
  3. bool useAnimateTo = false,
  4. double ruleOutSpacing = 0,
  5. Curve curve = Curves.linear,
})

跳转至指定 index

Implementation

Future<void> jumpToIndex(int index,
    {Duration duration = const Duration(milliseconds: 20),
    bool useAnimateTo = false,
    double ruleOutSpacing = 0,
    Curve curve = Curves.linear}) async {
  if (index >= _keyList.length) index = _keyList.length - 1;
  if (index < 1) index = 1;
  if (_ancestor == null) jumpTimes = 0;
  _ancestor ??= _scrollKey!.currentContext!.findRenderObject();
  final targetKey = _keyList[index];
  GlobalKey? jumpKey;
  if (targetKey.currentContext == null) {
    if (index < lastIndex) {
      jumpKey = _keyList.where((e) => e.currentContext != null).first;
    } else {
      jumpKey = _keyList.where((e) => e.currentContext != null).last;
    }
    final jumpIndex = _keyList.indexOf(jumpKey);
    final value = useAnimateTo
        ? await _animateTo(jumpKey.currentContext!,
            duration: duration, curve: curve, ruleOutSpacing: ruleOutSpacing)
        : _jumpTo(jumpKey.currentContext!, ruleOutSpacing: ruleOutSpacing);
    if (value) {
      lastIndex = jumpIndex;
      await duration.delayed(() async => await jumpToIndex(index));
    }
  } else {
    lastIndex = index;
    useAnimateTo
        ? await _animateTo(targetKey.currentContext!,
            duration: duration,
            curve: curve,
            isEnd: true,
            ruleOutSpacing: ruleOutSpacing)
        : _jumpTo(targetKey.currentContext!,
            isEnd: true, ruleOutSpacing: ruleOutSpacing);
    jumpTimes = 0;
  }
}