Skip to content

Commit 444f9c8

Browse files
committed
Add mouseCursor to RawScrollbar
Introduce a WidgetStateProperty<MouseCursor?>-based override on RawScrollbar so callers can drive the cursor over the scrollbar's thumb/track from hovered/dragged WidgetStates. The override is gated to apply only while the pointer is over the painted scrollbar or while a thumb drag is in progress, so ancestor cursors (e.g. a TextField's I-beam) remain in effect over non-scrollbar regions. Keeping the override applied while a drag is active prevents the cursor from flickering back to the ancestor cursor if the pointer briefly leaves the painted track during a drag, and matches the typical native behavior of grab/grabbing cursors. To avoid spurious rebuilds, only call updateMouseCursor() when the hover-active state actually changes, and reset _dragIsActive (and re-resolve the cursor) when the thumb drag is cancelled by another gesture taking over. The Scrollbar/ScrollbarThemeData surfacing of this property in the material library is omitted here due to the active material/cupertino code freeze (flutter#184093) and will be ported to material_ui in flutter/packages once that repo opens for changes, per the freeze guidance. Refs flutter#185733
1 parent 23756b2 commit 444f9c8

2 files changed

Lines changed: 303 additions & 0 deletions

File tree

‎packages/flutter/lib/src/widgets/scrollbar.dart‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import 'scroll_position.dart';
3636
import 'scrollable.dart';
3737
import 'scrollable_helpers.dart';
3838
import 'ticker_provider.dart';
39+
import 'widget_state.dart';
3940

4041
const double _kMinThumbExtent = 18.0;
4142
const double _kMinInteractiveSize = 48.0;
@@ -1010,6 +1011,7 @@ class RawScrollbar extends StatefulWidget {
10101011
this.mainAxisMargin = 0.0,
10111012
this.crossAxisMargin = 0.0,
10121013
this.padding,
1014+
this.mouseCursor,
10131015
}) : assert(
10141016
!(thumbVisibility == false && (trackVisibility ?? false)),
10151017
'A scrollbar track cannot be drawn without a scrollbar thumb.',
@@ -1343,6 +1345,28 @@ class RawScrollbar extends StatefulWidget {
13431345
/// Defaults to null.
13441346
final EdgeInsetsGeometry? padding;
13451347

1348+
/// {@template flutter.widgets.Scrollbar.mouseCursor}
1349+
/// The cursor for a mouse pointer when it enters or is hovering over the
1350+
/// scrollbar's thumb or track.
1351+
///
1352+
/// Resolves in the following [WidgetState]s:
1353+
/// * [WidgetState.dragged].
1354+
/// * [WidgetState.hovered].
1355+
///
1356+
/// The cursor is applied while the pointer is over the painted scrollbar
1357+
/// (using the same hit test area as the hover interaction) and while the
1358+
/// scrollbar thumb is being dragged, so the dragged cursor (typically
1359+
/// [SystemMouseCursors.grabbing]) remains stable even if the pointer
1360+
/// briefly moves outside the scrollbar during a drag. When neither
1361+
/// condition holds, cursor resolution is deferred to ancestors, so a
1362+
/// parent widget's cursor (for example, the I-beam cursor of an enclosing
1363+
/// [TextField]) is preserved.
1364+
///
1365+
/// When this is null, or when its resolved value is null, [MouseCursor.defer]
1366+
/// is used and ancestor cursors are left unchanged.
1367+
/// {@endtemplate}
1368+
final WidgetStateProperty<MouseCursor?>? mouseCursor;
1369+
13461370
@override
13471371
RawScrollbarState<RawScrollbar> createState() => RawScrollbarState<RawScrollbar>();
13481372
}
@@ -1364,6 +1388,8 @@ class RawScrollbarState<T extends RawScrollbar> extends State<T> with TickerProv
13641388
late CurvedAnimation _fadeoutOpacityAnimation;
13651389
final GlobalKey _scrollbarPainterKey = GlobalKey();
13661390
bool _hoverIsActive = false;
1391+
bool _dragIsActive = false;
1392+
MouseCursor _effectiveMouseCursor = MouseCursor.defer;
13671393
Drag? _thumbDrag;
13681394
bool _maxScrollExtentPermitsScrolling = false;
13691395
ScrollHoldController? _thumbHold;
@@ -1606,6 +1632,9 @@ class RawScrollbarState<T extends RawScrollbar> extends State<T> with TickerProv
16061632
_fadeoutAnimationController.reverse();
16071633
}
16081634
}
1635+
if (widget.mouseCursor != oldWidget.mouseCursor) {
1636+
updateMouseCursor();
1637+
}
16091638
}
16101639

16111640
void _maybeStartFadeoutTimer() {
@@ -1747,6 +1776,8 @@ class RawScrollbarState<T extends RawScrollbar> extends State<T> with TickerProv
17471776
_startDragScrollbarAxisOffset = localPosition;
17481777
_lastDragUpdateOffset = localPosition;
17491778
_startDragThumbOffset = scrollbarPainter.getThumbScrollOffset();
1779+
_dragIsActive = true;
1780+
updateMouseCursor();
17501781
}
17511782

17521783
/// Handler called when a currently active long press gesture moves.
@@ -1801,6 +1832,8 @@ class RawScrollbarState<T extends RawScrollbar> extends State<T> with TickerProv
18011832
@mustCallSuper
18021833
void handleThumbPressEnd(Offset localPosition, Velocity velocity) {
18031834
assert(_debugCheckHasValidScrollPosition());
1835+
_dragIsActive = false;
1836+
updateMouseCursor();
18041837
final Axis? direction = getScrollbarDirection();
18051838
if (direction == null) {
18061839
return;
@@ -2014,6 +2047,10 @@ class RawScrollbarState<T extends RawScrollbar> extends State<T> with TickerProv
20142047
_thumbDrag?.cancel();
20152048
assert(_thumbHold == null);
20162049
assert(_thumbDrag == null);
2050+
if (_dragIsActive) {
2051+
_dragIsActive = false;
2052+
updateMouseCursor();
2053+
}
20172054
}
20182055

20192056
void _initThumbDragGestureRecognizer(DragGestureRecognizer instance) {
@@ -2128,15 +2165,20 @@ class RawScrollbarState<T extends RawScrollbar> extends State<T> with TickerProv
21282165
void handleHover(PointerHoverEvent event) {
21292166
// Check if the position of the pointer falls over the painted scrollbar
21302167
if (isPointerOverScrollbar(event.position, event.kind, forHover: true)) {
2168+
final bool wasHoverActive = _hoverIsActive;
21312169
_hoverIsActive = true;
21322170
// Bring the scrollbar back into view if it has faded or started to fade
21332171
// away.
21342172
_fadeoutAnimationController.forward();
21352173
_fadeoutTimer?.cancel();
2174+
if (!wasHoverActive) {
2175+
updateMouseCursor();
2176+
}
21362177
} else if (_hoverIsActive) {
21372178
// Pointer is not over painted scrollbar.
21382179
_hoverIsActive = false;
21392180
_maybeStartFadeoutTimer();
2181+
updateMouseCursor();
21402182
}
21412183
}
21422184

@@ -2148,6 +2190,37 @@ class RawScrollbarState<T extends RawScrollbar> extends State<T> with TickerProv
21482190
void handleHoverExit(PointerExitEvent event) {
21492191
_hoverIsActive = false;
21502192
_maybeStartFadeoutTimer();
2193+
updateMouseCursor();
2194+
}
2195+
2196+
/// The set of [WidgetState]s used to resolve [RawScrollbar.mouseCursor].
2197+
///
2198+
/// Subclasses can override this to add additional states.
2199+
@protected
2200+
Set<WidgetState> get cursorStates => <WidgetState>{
2201+
if (_hoverIsActive) WidgetState.hovered,
2202+
if (_dragIsActive) WidgetState.dragged,
2203+
};
2204+
2205+
/// Recomputes the cursor applied by the [MouseRegion] wrapping the
2206+
/// scrollbar. The cursor is only overridden while the pointer is over the
2207+
/// painted scrollbar; otherwise [MouseCursor.defer] is used so that
2208+
/// ancestor cursors (such as a [TextField]'s I-beam) are preserved.
2209+
///
2210+
/// Subclasses should call this after changes to state that affect cursor
2211+
/// resolution (for example, when entering or leaving a drag).
2212+
@protected
2213+
@mustCallSuper
2214+
void updateMouseCursor() {
2215+
final MouseCursor? resolved = widget.mouseCursor?.resolve(cursorStates);
2216+
final MouseCursor desired = ((_hoverIsActive || _dragIsActive) && resolved != null)
2217+
? resolved
2218+
: MouseCursor.defer;
2219+
if (desired != _effectiveMouseCursor) {
2220+
setState(() {
2221+
_effectiveMouseCursor = desired;
2222+
});
2223+
}
21512224
}
21522225

21532226
// Returns the delta that should result from applying [event] with axis and
@@ -2237,6 +2310,7 @@ class RawScrollbarState<T extends RawScrollbar> extends State<T> with TickerProv
22372310
key: _gestureDetectorKey,
22382311
gestures: _gestures,
22392312
child: MouseRegion(
2313+
cursor: _effectiveMouseCursor,
22402314
onExit: (PointerExitEvent event) {
22412315
switch (event.kind) {
22422316
case PointerDeviceKind.mouse:

0 commit comments

Comments
 (0)