Skip to content

Commit

Permalink
Merge pull request #3 from vishnukvmd/patch-1
Browse files Browse the repository at this point in the history
Provide the latest scroll index to DraggableScrollbar
  • Loading branch information
deakjahn authored Apr 20, 2021
2 parents a3f1c3e + 98ff789 commit be55f05
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
7 changes: 5 additions & 2 deletions lib/src/draggable_scrollbar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class DraggableScrollbar extends StatefulWidget {

class DraggableScrollbarState extends State<DraggableScrollbar> with TickerProviderStateMixin {
double thumbOffset = 0.0;
int currentFirstIndex;
bool isDragging = false;

double get thumbMin => 0.0;
Expand All @@ -45,6 +46,7 @@ class DraggableScrollbarState extends State<DraggableScrollbar> with TickerProvi
void initState() {
super.initState();

this.currentFirstIndex = widget.currentFirstIndex;
if (widget.initialScrollIndex > 0 && widget.totalCount > 1) {
WidgetsBinding.instance?.addPostFrameCallback((_) {
setState(() => thumbOffset = (widget.initialScrollIndex / widget.totalCount) * (thumbMax - thumbMin));
Expand Down Expand Up @@ -79,12 +81,13 @@ class DraggableScrollbarState extends State<DraggableScrollbar> with TickerProvi
alignment: Alignment.topRight,
margin: EdgeInsets.only(top: thumbOffset),
padding: widget.padding,
child: widget.scrollThumbBuilder.call(widget.backgroundColor, widget.drawColor, widget.heightScrollThumb, widget.currentFirstIndex),
child: widget.scrollThumbBuilder.call(widget.backgroundColor, widget.drawColor, widget.heightScrollThumb, this.currentFirstIndex),
),
);

void setPosition(double position) {
void setPosition(double position, int currentFirstIndex) {
setState(() {
this.currentFirstIndex = currentFirstIndex;
thumbOffset = position * (thumbMax - thumbMin);
});
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/huge_listview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class HugeListViewState<T> extends State<HugeListView<T>> {
void _sendScroll() {
int current = _currentFirst();
widget.firstShown?.call(current);
scrollKey.currentState?.setPosition(current / widget.totalCount);
scrollKey.currentState?.setPosition(current / widget.totalCount, current);
}

int _currentFirst() {
Expand Down Expand Up @@ -212,7 +212,7 @@ class HugeListViewState<T> extends State<HugeListView<T>> {
/// Jump to the [position] in the list. [position] is between 0.0 (first item) and 1.0 (last item), practically currentIndex / totalCount.
/// To jump to a specific item, use [ItemScrollController.jumpTo] or [ItemScrollController.scrollTo].
void setPosition(double position) {
scrollKey.currentState?.setPosition(position);
scrollKey.currentState?.setPosition(position, _currentFirst());
}
}

Expand Down

1 comment on commit be55f05

@deakjahn
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The this in line 84 is actually superfluous, pedantic signals it, but not a problem. I'll remove it the next time around.

Please sign in to comment.