scrollTo function

void scrollTo(
  1. num? x,
  2. num? y, {
  3. bool smooth = true,
  4. int? delayMs,
  5. Object? scrollable,
})

Scrolls viewport to x,y.

  • If smooth is true will animate the scroll.
  • If delayMs >= 1 it will scroll after a Future.delayed. (value in milliseconds)
  • scrollable is the element to scroll. If null it will be the window or the body, identifying which one is scrolled.

Implementation

void scrollTo(
  num? x,
  num? y, {
  bool smooth = true,
  int? delayMs,
  Object? scrollable,
}) {
  if (delayMs != null && delayMs > 0) {
    _callAsync(
      delayMs,
      () => scrollTo(x, y, smooth: smooth, scrollable: scrollable),
    );
    return;
  }

  var scrollableResolved = _resolveScrollable(scrollable);

  var params = JSObject();
  if (x != null) params.setProperty('left'.toJS, x.toInt().toJS);
  if (y != null) params.setProperty('top'.toJS, y.toInt().toJS);
  if (smooth) params.setProperty('behavior'.toJS, 'smooth'.toJS);

  if (scrollableResolved.isA<Window>()) {
    (scrollableResolved as Window).scrollTo(params);
  } else if (scrollableResolved.isA<Element>()) {
    (scrollableResolved as Element).scrollTo(params);
  } else {
    window.scrollTo(params);
  }
}