diff --git a/app/lib/common/widgets/scrollable_stack_with_indicator.dart b/app/lib/common/widgets/scrollable_stack_with_indicator.dart index 4f8fd7fa..e26ff117 100644 --- a/app/lib/common/widgets/scrollable_stack_with_indicator.dart +++ b/app/lib/common/widgets/scrollable_stack_with_indicator.dart @@ -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; @@ -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(1); final scrollController = useScrollController( @@ -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; + } } } @@ -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( @@ -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; + } }, ), ),