scrollToId method

void scrollToId(
  1. String id
)

Scrolls so the anchor with id is centered in the viewport. Selecting the last item re-attaches follow (source rail last-item path).

Implementation

void scrollToId(String id) {
  final items = _resolvedRailItems();
  if (items.isEmpty) return;
  final lastId = items.last.id;
  if (id == lastId) {
    _setFollowing(true);
    scrollToEnd(smooth: widget.smooth);
    setState(() => _activeRailId = id);
    return;
  }

  final entry = _anchors[id];
  final ctx = entry?.key.currentContext;
  if (ctx == null) return;

  _setFollowing(false);
  setState(() => _activeRailId = id);
  final token = ++_scrollToken;
  _programmaticClear?.cancel();
  _programmatic = true;

  final reduce = MediaQuery.disableAnimationsOf(context);
  final useSmooth = widget.smooth && !reduce;
  Scrollable.ensureVisible(
    ctx,
    alignment: 0.5,
    duration: useSmooth ? _followDuration : Duration.zero,
    curve: Curves.easeOut,
  ).whenComplete(() => _clearProgrammatic(token));
  if (useSmooth) {
    _programmaticClear = Timer(
      _followDuration,
      () => _clearProgrammatic(token),
    );
  } else {
    _clearProgrammatic(token);
  }
}