@@ -33,10 +33,7 @@ class ScrollableStackWithIndicator extends HookWidget {
33
33
return scrollPosition.pixels >= maxScrollOffset;
34
34
}
35
35
36
- double ? _getRelativeScrollPosition (
37
- GlobalKey contentKey,
38
- ScrollPosition scrollPosition,
39
- ) {
36
+ double ? _getRelativeScrollPosition (ScrollPosition scrollPosition) {
40
37
final maxScrollOffset = scrollPosition.maxScrollExtent;
41
38
final relativePosition =
42
39
1 - (maxScrollOffset - scrollPosition.pixels) / maxScrollOffset;
@@ -53,6 +50,7 @@ class ScrollableStackWithIndicator extends HookWidget {
53
50
final scrollbarPadding = rightScrollbarPadding ?? PharMeTheme .smallSpace;
54
51
final horizontalPadding = scrollbarPadding + 3 * scrollbarThickness;
55
52
final contentKey = GlobalKey ();
53
+ final failedIndicatorInitializationAttempts = useState (0 );
56
54
final showScrollIndicatorButton = useState (false );
57
55
final scrollIndicatorButtonOpacity = useState <double >(1 );
58
56
final scrollController = useScrollController (
@@ -61,12 +59,14 @@ class ScrollableStackWithIndicator extends HookWidget {
61
59
);
62
60
63
61
void handleScrolling () {
64
- final hideButton = _scrolledToEnd (scrollController.position) ?? false ;
65
- showScrollIndicatorButton.value = ! hideButton;
66
- final relativeScrollPosition =
67
- _getRelativeScrollPosition (contentKey, scrollController.position);
68
- if (relativeScrollPosition != null ) {
69
- scrollIndicatorButtonOpacity.value = 1 - relativeScrollPosition;
62
+ if (scrollController.hasClients) {
63
+ final hideButton = _scrolledToEnd (scrollController.position) ?? false ;
64
+ showScrollIndicatorButton.value = ! hideButton;
65
+ final relativeScrollPosition =
66
+ _getRelativeScrollPosition (scrollController.position);
67
+ if (relativeScrollPosition != null ) {
68
+ scrollIndicatorButtonOpacity.value = 1 - relativeScrollPosition;
69
+ }
70
70
}
71
71
}
72
72
@@ -76,11 +76,19 @@ class ScrollableStackWithIndicator extends HookWidget {
76
76
}, [scrollController]);
77
77
78
78
WidgetsBinding .instance.addPostFrameCallback ((_) {
79
- final contentScrollable =
80
- _contentScrollable (contentKey, scrollController.position) ?? false ;
81
- final scrolledToEnd =
82
- _scrolledToEnd (scrollController.position) ?? false ;
83
- showScrollIndicatorButton.value = contentScrollable && ! scrolledToEnd;
79
+ try {
80
+ final contentScrollable =
81
+ _contentScrollable (contentKey, scrollController.position);
82
+ if (contentScrollable == null ) {
83
+ failedIndicatorInitializationAttempts.value += 1 ;
84
+ return ;
85
+ }
86
+ final scrolledToEnd =
87
+ _scrolledToEnd (scrollController.position) ?? false ;
88
+ showScrollIndicatorButton.value = contentScrollable && ! scrolledToEnd;
89
+ } catch (exception) {
90
+ failedIndicatorInitializationAttempts.value += 1 ;
91
+ }
84
92
});
85
93
86
94
return Stack (
@@ -130,12 +138,14 @@ class ScrollableStackWithIndicator extends HookWidget {
130
138
color: iconColor ?? PharMeTheme .iconColor,
131
139
),
132
140
onPressed: () async {
133
- await scrollController.animateTo (
134
- scrollController.position.maxScrollExtent,
135
- duration: Duration (milliseconds: 500 ),
136
- curve: Curves .linearToEaseOut,
137
- );
138
- showScrollIndicatorButton.value = false ;
141
+ if (scrollController.hasClients) {
142
+ await scrollController.animateTo (
143
+ scrollController.position.maxScrollExtent,
144
+ duration: Duration (milliseconds: 500 ),
145
+ curve: Curves .linearToEaseOut,
146
+ );
147
+ showScrollIndicatorButton.value = false ;
148
+ }
139
149
},
140
150
),
141
151
),
0 commit comments