Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correct offset when scrollController with initialScrollOffset #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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