Skip to content

[scrollable_positioned_list] Add ScrollController to be observed by Scrollbar #483

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class ScrollablePositionedList extends StatefulWidget {
required this.itemBuilder,
Key? key,
this.itemScrollController,
this.scrollController,
this.shrinkWrap = false,
ItemPositionsListener? itemPositionsListener,
this.scrollOffsetController,
Expand Down Expand Up @@ -73,6 +74,7 @@ class ScrollablePositionedList extends StatefulWidget {
Key? key,
this.shrinkWrap = false,
this.itemScrollController,
this.scrollController,
ItemPositionsListener? itemPositionsListener,
this.scrollOffsetController,
ScrollOffsetListener? scrollOffsetListener,
Expand All @@ -94,6 +96,8 @@ class ScrollablePositionedList extends StatefulWidget {
scrollOffsetNotifier = scrollOffsetListener as ScrollOffsetNotifier?,
super(key: key);

final ScrollController? scrollController;

/// Number of items the [itemBuilder] can produce.
final int itemCount;

Expand Down Expand Up @@ -283,25 +287,36 @@ class ScrollOffsetController {
{required double offset,
required Duration duration,
Curve curve = Curves.linear}) async {
final currentPosition =
_scrollableListState!.primary.scrollController.offset;
final currentPosition =
scrollableListState!.primary.scrollController.offset;
final newPosition = currentPosition + offset;
await _scrollableListState!.primary.scrollController.animateTo(
await scrollableListState!.primary.scrollController.animateTo(
newPosition,
duration: duration,
curve: curve,
);
}

_ScrollablePositionedListState? _scrollableListState;
Future<void> animateScrollAbsolute(
{required double offset,
required Duration duration,
Curve curve = Curves.linear}) async {
await scrollableListState!.primary.scrollController.animateTo(
offset,
duration: duration,
curve: curve,
);
}

void _attach(_ScrollablePositionedListState scrollableListState) {
assert(_scrollableListState == null);
_scrollableListState = scrollableListState;
_ScrollablePositionedListState? scrollableListState;

void _attach(_ScrollablePositionedListState _scrollableListState) {
assert(scrollableListState == null);
scrollableListState = _scrollableListState;
}

void _detach() {
_scrollableListState = null;
scrollableListState = null;
}
}

Expand Down Expand Up @@ -405,6 +420,11 @@ class _ScrollablePositionedListState extends State<ScrollablePositionedList>

@override
Widget build(BuildContext context) {
if (widget.scrollController?.positions.isEmpty == true)
WidgetsBinding.instance.addPostFrameCallback((_) {
widget.scrollController?.attach(primary.scrollController.position);
});

return LayoutBuilder(
builder: (context, constraints) {
final cacheExtent = _cacheExtent(constraints);
Expand Down Expand Up @@ -610,6 +630,9 @@ class _ScrollablePositionedListState extends State<ScrollablePositionedList>
if (mounted) {
setState(() {
if (opacity.value >= 0.5) {
if (widget.scrollController?.position != null) {
widget.scrollController?.detach(widget.scrollController!.position);
}
// Secondary [ListView] is more visible than the primary; make it the
// new primary.
var temp = primary;
Expand Down