calculateScroll method

void calculateScroll({
  1. double? maxWidth,
})

Implementation

void calculateScroll({double? maxWidth}) {
  _maxScroll = maxWidth ?? _maxScroll;
  WidgetsBinding.instance.addPostFrameCallback(
    (_) {
      final maxScrollExtent = widget.scrollController.position.maxScrollExtent;
      final bool preStatusOfScrollBar = _statusOfScrollBar;
      if (maxScrollExtent.floor() == 0) {
        _statusOfScrollBar = false;
        if (preStatusOfScrollBar != _statusOfScrollBar) setState(() {});
        return;
      }
      _statusOfScrollBar = true;
      if (preStatusOfScrollBar != _statusOfScrollBar) setState(() {});
      final scrollControllerOffset = widget.scrollController.offset;
      _scrollWidth = (_maxScroll / (_maxScroll + maxScrollExtent)) * _maxScroll;
      _currentPosition = (scrollControllerOffset / maxScrollExtent) * (_maxScroll - _scrollWidth);
      if (_currentPosition >= _maxScroll - _scrollWidth) {
        _currentPosition = _maxScroll - _scrollWidth;
      }
      (_scrollGlobalKey.currentState as BasicHorizontalScrollWidgetState?)
          ?.setMaxScroll(_maxScroll);
      (_scrollGlobalKey.currentState as BasicHorizontalScrollWidgetState?)
          ?.setScrollWidth(_scrollWidth);
      (_scrollGlobalKey.currentState as BasicHorizontalScrollWidgetState?)
          ?.setCurrentPosition(_currentPosition);
    },
  );
}