scrollTo method

  1. @override
void scrollTo(
  1. String? id,
  2. String? value, {
  3. bool animate = false,
})
override

scroll +/- pixels or to an item

Implementation

@override
void scrollTo(String? id, String? value, {bool animate = false}) {

  if (id == null) return;

  // get the view
  ListLayoutViewState? view = findListenerOfExactType(ListLayoutViewState);

  // scroll to top
  if ((id.trim().toLowerCase() == 'top' || id.trim().toLowerCase() == 'start') && isNullOrEmpty(value)) {
    view?.scrollTo(0, animate: false);
    return;
  }

  // scroll to bottom
  if ((id.trim().toLowerCase() == 'bottom' || id.trim().toLowerCase() == 'end') && isNullOrEmpty(value)) {
    view?.scrollTo(double.maxFinite, animate: false);
    return;
  }

  // scroll to specific pixel position
  if (isNumeric(id) && isNullOrEmpty(value)) {
    view?.scrollTo(toDouble(id), animate: false);
  }

  // build out the items
  // this may lag the system if the list is large
  if (data != null && items.length != data.length) {
    for (int i = 0; i < data.length; i++) {
      if (!items.containsKey(i)) {
        var item = getItemModel(i);
        if (item != null) {
          items[i] = item;
        }
      }
    }
  }

  // find the first item containing a child with the specified
  // id and matching value
  for (var item in items.values) {
    var child = item.descendants?.toList().firstWhereOrNull((child) => child.id == id && child.value == (value ?? child.value));
    if (child != null) {

      // get the item's position in the list
      int i = items.values.toList().indexOf(item);

      // scroll to that item
      view?.scrollTo(i * itemExtent, animate: animate);
    }
  }
}