flutter-draggable-scrollbar icon indicating copy to clipboard operation
flutter-draggable-scrollbar copied to clipboard

correct offset when scrollController with initialScrollOffset

Open catoldcui opened this issue 5 years ago • 2 comments

Current issue

When scrollController inits with initialScrollOffset, the scroll bar will always be with offset 0, and could not scroll it up vertically.

Solution

Recalculate the offset when the widgets finish loading. Set the offset by initialScrollOffset.

catoldcui avatar Dec 17 '18 16:12 catoldcui

Please update the function like this :

void _initOffsetForScrollInitialOffset() {
    if(widget.controller.hasClients) {
      _viewOffset = widget.controller.initialScrollOffset;
      _barOffset = _viewOffset / viewMaxScrollExtent * barMaxScrollExtent;
    }
    setState(() {});
  }

Without widget.controller.hasClients following error is thrown :

═════════════════════════ Exception caught by scheduler library════════════════════════
The following assertion was thrown during a scheduler callback:
Assertion failed: file:///D:/flutter/packages/flutter/lib/src/widgets/scroll_controller.dart:108:12
_positions.isNotEmpty
"ScrollController not attached to any scroll views."

When the exception was thrown, this was the stack: 
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 216:49  throw_
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 24:3    assertFailed
packages/flutter/src/widgets/scroll_controller.dart 108:23                                                                 get position
packages/wenelsahra/external_lib/draggable_scrollbar.dart 373:64                                                           get viewMaxScrollExtent
packages/wenelsahra/external_lib/draggable_scrollbar.dart 357:32                                                           [_initOffsetForScrollInitialOffset]
...
═════════════════════════════════════════════════════════════════════════════

shatanikmahanty avatar Nov 30 '20 11:11 shatanikmahanty

This patch somehow generated NaN _barOffset for me, so I modified it like this:

  void _initOffsetForScrollInitialOffset() {
    if (widget.controller.hasClients) {
      _viewOffset = widget.controller.initialScrollOffset;
      _barOffset = _viewOffset / viewMaxScrollExtent * barMaxScrollExtent;
      if (_barOffset.isNaN || _barOffset < 0) _barOffset = 0;
    }
    if (mounted) setState(() {});
  }

emvaized avatar Dec 09 '21 22:12 emvaized