attemptCollapse method

bool attemptCollapse(
  1. int index,
  2. int direction
)

Implementation

bool attemptCollapse(int index, int direction) {
  if (index == 0) {
    direction = 1;
  } else if (index == items.length - 1) {
    direction = -1;
  }
  if (direction < 0) {
    final item = items[index];
    final collapsedSize = item.collapsedSize ?? 0;
    final currentSize = item.newValue;
    final delta = currentSize - collapsedSize;
    var borrowed = _borrowSize(index - 1, delta, 0, -1);
    if (borrowed.givenSize != delta) {
      reset();
      return false;
    }
    item._newCollapsed = true;
    return true;
  } else if (direction > 0) {
    final item = items[index];
    final collapsedSize = item.collapsedSize ?? 0;
    final delta = item.newValue - collapsedSize;
    var borrowed = _borrowSize(index + 1, delta, items.length - 1, 1);
    if (borrowed.givenSize != delta) {
      reset();
      return false;
    }
    item._newCollapsed = true;
    return true;
  } else if (direction == 0) {
    final item = items[index];
    final collapsedSize = item.collapsedSize ?? 0;
    final delta = item.newValue - collapsedSize;
    final halfDelta = delta / 2;
    var borrowedLeft = _borrowSize(index - 1, halfDelta, 0, -1);
    var borrowedRight =
        _borrowSize(index + 1, halfDelta, items.length - 1, 1);
    if (borrowedLeft.givenSize != halfDelta ||
        borrowedRight.givenSize != halfDelta) {
      reset();
      return false;
    }
    item._newCollapsed = true;
    return true;
  }
  return false;
}