Skip to content

Conversation

@catoldcui
Copy link

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.

@shatanikmahanty
Copy link

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]
...
═════════════════════════════════════════════════════════════════════════════

@emvaized
Copy link

emvaized commented Dec 9, 2021

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(() {});
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants