Skip to content

Commit

Permalink
fix(app): catch if the scroll controller does not have clients
Browse files Browse the repository at this point in the history
  • Loading branch information
tamslo committed Feb 28, 2025
1 parent 878e01d commit 47e0b6e
Showing 1 changed file with 31 additions and 21 deletions.
52 changes: 31 additions & 21 deletions app/lib/common/widgets/scrollable_stack_with_indicator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ class ScrollableStackWithIndicator extends HookWidget {
return scrollPosition.pixels >= maxScrollOffset;
}

double? _getRelativeScrollPosition(
GlobalKey contentKey,
ScrollPosition scrollPosition,
) {
double? _getRelativeScrollPosition(ScrollPosition scrollPosition) {
final maxScrollOffset = scrollPosition.maxScrollExtent;
final relativePosition =
1 - (maxScrollOffset - scrollPosition.pixels) / maxScrollOffset;
Expand All @@ -53,6 +50,7 @@ class ScrollableStackWithIndicator extends HookWidget {
final scrollbarPadding = rightScrollbarPadding ?? PharMeTheme.smallSpace;
final horizontalPadding = scrollbarPadding + 3 * scrollbarThickness;
final contentKey = GlobalKey();
final failedIndicatorInitializationAttempts = useState(0);
final showScrollIndicatorButton = useState(false);
final scrollIndicatorButtonOpacity = useState<double>(1);
final scrollController = useScrollController(
Expand All @@ -61,12 +59,14 @@ class ScrollableStackWithIndicator extends HookWidget {
);

void handleScrolling() {
final hideButton = _scrolledToEnd(scrollController.position) ?? false;
showScrollIndicatorButton.value = !hideButton;
final relativeScrollPosition =
_getRelativeScrollPosition(contentKey, scrollController.position);
if (relativeScrollPosition != null) {
scrollIndicatorButtonOpacity.value = 1 - relativeScrollPosition;
if (scrollController.hasClients) {
final hideButton = _scrolledToEnd(scrollController.position) ?? false;
showScrollIndicatorButton.value = !hideButton;
final relativeScrollPosition =
_getRelativeScrollPosition(scrollController.position);
if (relativeScrollPosition != null) {
scrollIndicatorButtonOpacity.value = 1 - relativeScrollPosition;
}
}
}

Expand All @@ -76,11 +76,19 @@ class ScrollableStackWithIndicator extends HookWidget {
}, [scrollController]);

WidgetsBinding.instance.addPostFrameCallback((_) {
final contentScrollable =
_contentScrollable(contentKey, scrollController.position) ?? false;
final scrolledToEnd =
_scrolledToEnd(scrollController.position) ?? false;
showScrollIndicatorButton.value = contentScrollable && !scrolledToEnd;
try {
final contentScrollable =
_contentScrollable(contentKey, scrollController.position);
if (contentScrollable == null) {
failedIndicatorInitializationAttempts.value += 1;
return;
}
final scrolledToEnd =
_scrolledToEnd(scrollController.position) ?? false;
showScrollIndicatorButton.value = contentScrollable && !scrolledToEnd;
} catch (exception) {
failedIndicatorInitializationAttempts.value += 1;
}
});

return Stack(
Expand Down Expand Up @@ -130,12 +138,14 @@ class ScrollableStackWithIndicator extends HookWidget {
color: iconColor ?? PharMeTheme.iconColor,
),
onPressed: () async {
await scrollController.animateTo(
scrollController.position.maxScrollExtent,
duration: Duration(milliseconds: 500),
curve: Curves.linearToEaseOut,
);
showScrollIndicatorButton.value = false;
if (scrollController.hasClients) {
await scrollController.animateTo(
scrollController.position.maxScrollExtent,
duration: Duration(milliseconds: 500),
curve: Curves.linearToEaseOut,
);
showScrollIndicatorButton.value = false;
}
},
),
),
Expand Down

0 comments on commit 47e0b6e

Please sign in to comment.