getStateFromOffset<T> method

int getStateFromOffset<T>(
  1. double offset,
  2. MultiStateSheetExtent<T> extent
)

Maps the given offset to its corresponding state index.

  • offset: The current offset of the sheet.
  • extent: The current extent of the sheet.
  • Returns: The state index corresponding to the given offset.

Implementation

int getStateFromOffset<T>(double offset, MultiStateSheetExtent<T> extent) =>
    cachedStateFromOffset.putIfAbsent(offset, () {
      final stateIndex = stateOfOffset(offset);

      if (stateIndex == -1) {
        final minPosition = extent.minOffset;
        final maxPosition = extent.safeMaxOffset;

        final firstPosition = getFirstOffsetAfter(offset);
        final lastPosition = getLastOffsetBefore(offset);

        if ((offset - clampDouble(lastPosition, minPosition, maxPosition))
                .abs() >
            (offset - clampDouble(firstPosition, minPosition, maxPosition))
                .abs()) {
          return stateOfOffset(firstPosition);
        }
        return stateOfOffset(lastPosition);
      }
      return stateIndex;
    });