setSublocationId method

void setSublocationId(
  1. int newSublocationId
)

Updates selection by sublocation id and scrolls to it if needed.

Implementation

void setSublocationId(int newSublocationId) {
  final int newIndex = _floors.indexWhere((floor) => floor.sublocationId == newSublocationId);

  if (newIndex == -1) {
    return;
  }

  final bool wasAlreadySelected = _selectedFloorIndex == newIndex;

  if (!wasAlreadySelected) {
    setState(() {
      _selectedFloorIndex = newIndex;
    });
  } else {
    setState(() {});
  }

  _updateScrollButtonsVisibility();

  if (_scrollController.hasClients) {
    final bool needAnimation = _floors.length > kMaxVisibleFloors;

    final double targetOffset = newIndex * kFloorRowHeight;
    final double maxScroll = _scrollController.position.maxScrollExtent;
    final double clampedOffset = targetOffset.clamp(0.0, maxScroll);

    if (needAnimation) {
      _scrollController.animateTo(
        clampedOffset,
        duration: kScrollAnimationDuration,
        curve: kScrollAnimationCurve,
      );
    } else {
      _scrollController.jumpTo(clampedOffset);
    }
  }

  final selectedLevel = _floors[newIndex];
  widget.onFloorSelected?.call(selectedLevel.sublocationId, selectedLevel.levelId);
}