@@ -14,6 +14,7 @@ class StyledScrollbar extends StatefulWidget {
1414 final ScrollController controller;
1515 final Function (double )? onDrag;
1616 final bool showTrack;
17+ final bool autoHideScrollbar;
1718 final Color ? handleColor;
1819 final Color ? trackColor;
1920
@@ -30,6 +31,7 @@ class StyledScrollbar extends StatefulWidget {
3031 this .onDrag,
3132 this .contentSize,
3233 this .showTrack = false ,
34+ this .autoHideScrollbar = true ,
3335 this .handleColor,
3436 this .trackColor})
3537 : super (key: key);
@@ -48,16 +50,13 @@ class ScrollbarState extends State<StyledScrollbar> {
4850 widget.controller.addListener (() => setState (() {}));
4951 widget.controller.position.isScrollingNotifier.addListener (
5052 () {
51- if (! mounted) {
52- return ;
53- }
53+ if (! mounted) return ;
54+ if (! widget.autoHideScrollbar) return ;
5455 _hideScrollbarOperation? .cancel ();
5556 if (! widget.controller.position.isScrollingNotifier.value) {
56- // scroll is stopped
5757 _hideScrollbarOperation = CancelableOperation .fromFuture (
5858 Future .delayed (const Duration (seconds: 2 ), () {}),
5959 ).then ((_) {
60- // Opti: hide with animation
6160 hideHandler = true ;
6261 if (mounted) {
6362 setState (() {});
@@ -103,7 +102,6 @@ class ScrollbarState extends State<StyledScrollbar> {
103102 break ;
104103 case Axis .horizontal:
105104 // Use supplied contentSize if we have it, otherwise just fallback to maxScrollExtents
106-
107105 if (contentSize != null && contentSize > 0 ) {
108106 maxExtent = contentSize - constraints.maxWidth;
109107 } else {
@@ -118,7 +116,8 @@ class ScrollbarState extends State<StyledScrollbar> {
118116 // Calculate the alignment for the handle, this is a value between 0 and 1,
119117 // it automatically takes the handle size into acct
120118 // ignore: omit_local_variable_types
121- double handleAlignment = maxExtent == 0 ? 0 : widget.controller.offset / maxExtent;
119+ double handleAlignment =
120+ maxExtent == 0 ? 0 : widget.controller.offset / maxExtent;
122121
123122 // Convert handle alignment from [0, 1] to [-1, 1]
124123 handleAlignment *= 2.0 ;
@@ -127,19 +126,25 @@ class ScrollbarState extends State<StyledScrollbar> {
127126 // Calculate handleSize by comparing the total content size to our viewport
128127 var handleExtent = _viewExtent;
129128 if (contentExtent > _viewExtent) {
130- //Make sure handle is never small than the minSize
129+ // Make sure handle is never small than the minSize
131130 handleExtent = max (60 , _viewExtent * _viewExtent / contentExtent);
132131 }
132+
133133 // Hide the handle if content is < the viewExtent
134134 var showHandle = contentExtent > _viewExtent && contentExtent > 0 ;
135+
135136 if (hideHandler) {
136137 showHandle = false ;
137138 }
138139
139140 // Handle color
140- var handleColor = widget.handleColor ?? (theme.isDark ? theme.bg2.withOpacity (.2 ) : theme.bg2);
141+ var handleColor = widget.handleColor ??
142+ (theme.isDark ? theme.bg2.withOpacity (.2 ) : theme.bg2);
141143 // Track color
142- var trackColor = widget.trackColor ?? (theme.isDark ? theme.bg2.withOpacity (.1 ) : theme.bg2.withOpacity (.3 ));
144+ var trackColor = widget.trackColor ??
145+ (theme.isDark
146+ ? theme.bg2.withOpacity (.1 )
147+ : theme.bg2.withOpacity (.3 ));
143148
144149 //Layout the stack, it just contains a child, and
145150 return Stack (children: < Widget > [
@@ -149,8 +154,12 @@ class ScrollbarState extends State<StyledScrollbar> {
149154 alignment: const Alignment (1 , 1 ),
150155 child: Container (
151156 color: trackColor,
152- width: widget.axis == Axis .vertical ? widget.size : double .infinity,
153- height: widget.axis == Axis .horizontal ? widget.size : double .infinity,
157+ width: widget.axis == Axis .vertical
158+ ? widget.size
159+ : double .infinity,
160+ height: widget.axis == Axis .horizontal
161+ ? widget.size
162+ : double .infinity,
154163 ),
155164 ),
156165
@@ -167,10 +176,14 @@ class ScrollbarState extends State<StyledScrollbar> {
167176 // HANDLE SHAPE
168177 child: MouseHoverBuilder (
169178 builder: (_, isHovered) => Container (
170- width: widget.axis == Axis .vertical ? widget.size : handleExtent,
171- height: widget.axis == Axis .horizontal ? widget.size : handleExtent,
179+ width:
180+ widget.axis == Axis .vertical ? widget.size : handleExtent,
181+ height: widget.axis == Axis .horizontal
182+ ? widget.size
183+ : handleExtent,
172184 decoration: BoxDecoration (
173- color: handleColor.withOpacity (isHovered ? 1 : .85 ), borderRadius: Corners .s3Border),
185+ color: handleColor.withOpacity (isHovered ? 1 : .85 ),
186+ borderRadius: Corners .s3Border),
174187 ),
175188 ),
176189 ),
@@ -182,15 +195,19 @@ class ScrollbarState extends State<StyledScrollbar> {
182195
183196 void _handleHorizontalDrag (DragUpdateDetails details) {
184197 var pos = widget.controller.offset;
185- var pxRatio = (widget.controller.position.maxScrollExtent + _viewExtent) / _viewExtent;
186- widget.controller.jumpTo ((pos + details.delta.dx * pxRatio).clamp (0.0 , widget.controller.position.maxScrollExtent));
198+ var pxRatio = (widget.controller.position.maxScrollExtent + _viewExtent) /
199+ _viewExtent;
200+ widget.controller.jumpTo ((pos + details.delta.dx * pxRatio)
201+ .clamp (0.0 , widget.controller.position.maxScrollExtent));
187202 widget.onDrag? .call (details.delta.dx);
188203 }
189204
190205 void _handleVerticalDrag (DragUpdateDetails details) {
191206 var pos = widget.controller.offset;
192- var pxRatio = (widget.controller.position.maxScrollExtent + _viewExtent) / _viewExtent;
193- widget.controller.jumpTo ((pos + details.delta.dy * pxRatio).clamp (0.0 , widget.controller.position.maxScrollExtent));
207+ var pxRatio = (widget.controller.position.maxScrollExtent + _viewExtent) /
208+ _viewExtent;
209+ widget.controller.jumpTo ((pos + details.delta.dy * pxRatio)
210+ .clamp (0.0 , widget.controller.position.maxScrollExtent));
194211 widget.onDrag? .call (details.delta.dy);
195212 }
196213}
@@ -204,6 +221,7 @@ class ScrollbarListStack extends StatelessWidget {
204221 final EdgeInsets ? scrollbarPadding;
205222 final Color ? handleColor;
206223 final Color ? trackColor;
224+ final bool autoHideScrollbar;
207225
208226 const ScrollbarListStack (
209227 {Key ? key,
@@ -214,6 +232,7 @@ class ScrollbarListStack extends StatelessWidget {
214232 this .contentSize,
215233 this .scrollbarPadding,
216234 this .handleColor,
235+ this .autoHideScrollbar = true ,
217236 this .trackColor})
218237 : super (key: key);
219238
@@ -238,6 +257,7 @@ class ScrollbarListStack extends StatelessWidget {
238257 contentSize: contentSize,
239258 trackColor: trackColor,
240259 handleColor: handleColor,
260+ autoHideScrollbar: autoHideScrollbar,
241261 ),
242262 )
243263 // The animate will be used by the children that using styled_widget.
0 commit comments