Skip to content

Commit cc9af79

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 only apply while the pointer is over the painted scrollbar, so ancestor cursors (e.g. a TextField's I-beam) remain in effect over non-scrollbar regions. 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 fe517fc commit cc9af79

2 files changed

Lines changed: 218 additions & 0 deletions

File tree

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

Lines changed: 63 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,25 @@ 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 only applied while the pointer is over the painted
1357+
/// scrollbar (using the same hit test area as the hover interaction). When
1358+
/// the pointer is over the [child] but not over the scrollbar, cursor
1359+
/// resolution is deferred to ancestors, so a parent widget's cursor (for
1360+
/// example, the I-beam cursor of an enclosing [TextField]) is preserved.
1361+
///
1362+
/// When this is null, or when its resolved value is null, [MouseCursor.defer]
1363+
/// is used and ancestor cursors are left unchanged.
1364+
/// {@endtemplate}
1365+
final WidgetStateProperty<MouseCursor?>? mouseCursor;
1366+
13461367
@override
13471368
RawScrollbarState<RawScrollbar> createState() => RawScrollbarState<RawScrollbar>();
13481369
}
@@ -1364,6 +1385,8 @@ class RawScrollbarState<T extends RawScrollbar> extends State<T> with TickerProv
13641385
late CurvedAnimation _fadeoutOpacityAnimation;
13651386
final GlobalKey _scrollbarPainterKey = GlobalKey();
13661387
bool _hoverIsActive = false;
1388+
bool _dragIsActive = false;
1389+
MouseCursor _effectiveMouseCursor = MouseCursor.defer;
13671390
Drag? _thumbDrag;
13681391
bool _maxScrollExtentPermitsScrolling = false;
13691392
ScrollHoldController? _thumbHold;
@@ -1606,6 +1629,9 @@ class RawScrollbarState<T extends RawScrollbar> extends State<T> with TickerProv
16061629
_fadeoutAnimationController.reverse();
16071630
}
16081631
}
1632+
if (widget.mouseCursor != oldWidget.mouseCursor) {
1633+
updateMouseCursor();
1634+
}
16091635
}
16101636

16111637
void _maybeStartFadeoutTimer() {
@@ -1747,6 +1773,8 @@ class RawScrollbarState<T extends RawScrollbar> extends State<T> with TickerProv
17471773
_startDragScrollbarAxisOffset = localPosition;
17481774
_lastDragUpdateOffset = localPosition;
17491775
_startDragThumbOffset = scrollbarPainter.getThumbScrollOffset();
1776+
_dragIsActive = true;
1777+
updateMouseCursor();
17501778
}
17511779

17521780
/// Handler called when a currently active long press gesture moves.
@@ -1801,6 +1829,8 @@ class RawScrollbarState<T extends RawScrollbar> extends State<T> with TickerProv
18011829
@mustCallSuper
18021830
void handleThumbPressEnd(Offset localPosition, Velocity velocity) {
18031831
assert(_debugCheckHasValidScrollPosition());
1832+
_dragIsActive = false;
1833+
updateMouseCursor();
18041834
final Axis? direction = getScrollbarDirection();
18051835
if (direction == null) {
18061836
return;
@@ -2138,6 +2168,7 @@ class RawScrollbarState<T extends RawScrollbar> extends State<T> with TickerProv
21382168
_hoverIsActive = false;
21392169
_maybeStartFadeoutTimer();
21402170
}
2171+
updateMouseCursor();
21412172
}
21422173

21432174
/// Initiates the fade out animation.
@@ -2148,6 +2179,37 @@ class RawScrollbarState<T extends RawScrollbar> extends State<T> with TickerProv
21482179
void handleHoverExit(PointerExitEvent event) {
21492180
_hoverIsActive = false;
21502181
_maybeStartFadeoutTimer();
2182+
updateMouseCursor();
2183+
}
2184+
2185+
/// The set of [WidgetState]s used to resolve [RawScrollbar.mouseCursor].
2186+
///
2187+
/// Subclasses can override this to add additional states.
2188+
@protected
2189+
Set<WidgetState> get cursorStates => <WidgetState>{
2190+
if (_hoverIsActive) WidgetState.hovered,
2191+
if (_dragIsActive) WidgetState.dragged,
2192+
};
2193+
2194+
/// Recomputes the cursor applied by the [MouseRegion] wrapping the
2195+
/// scrollbar. The cursor is only overridden while the pointer is over the
2196+
/// painted scrollbar; otherwise [MouseCursor.defer] is used so that
2197+
/// ancestor cursors (such as a [TextField]'s I-beam) are preserved.
2198+
///
2199+
/// Subclasses should call this after changes to state that affect cursor
2200+
/// resolution (for example, when entering or leaving a drag).
2201+
@protected
2202+
@mustCallSuper
2203+
void updateMouseCursor() {
2204+
final MouseCursor? resolved = widget.mouseCursor?.resolve(cursorStates);
2205+
final MouseCursor desired = (_hoverIsActive && resolved != null)
2206+
? resolved
2207+
: MouseCursor.defer;
2208+
if (desired != _effectiveMouseCursor) {
2209+
setState(() {
2210+
_effectiveMouseCursor = desired;
2211+
});
2212+
}
21512213
}
21522214

21532215
// Returns the delta that should result from applying [event] with axis and
@@ -2237,6 +2299,7 @@ class RawScrollbarState<T extends RawScrollbar> extends State<T> with TickerProv
22372299
key: _gestureDetectorKey,
22382300
gestures: _gestures,
22392301
child: MouseRegion(
2302+
cursor: _effectiveMouseCursor,
22402303
onExit: (PointerExitEvent event) {
22412304
switch (event.kind) {
22422305
case PointerDeviceKind.mouse:

‎packages/flutter/test/widgets/scrollbar_test.dart‎

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'dart:ui' as ui;
66

77
import 'package:flutter/foundation.dart';
88
import 'package:flutter/material.dart';
9+
import 'package:flutter/rendering.dart';
910
import 'package:flutter/src/physics/utils.dart' show nearEqual;
1011
import 'package:flutter_test/flutter_test.dart';
1112

@@ -3801,4 +3802,158 @@ The provided ScrollController cannot be shared by multiple ScrollView widgets.''
38013802
expect(scrollController.offset, greaterThan(0.0));
38023803
},
38033804
);
3805+
3806+
testWidgets(
3807+
'RawScrollbar.mouseCursor only applies while the pointer is over the scrollbar',
3808+
(WidgetTester tester) async {
3809+
final controller = ScrollController();
3810+
addTearDown(controller.dispose);
3811+
3812+
await tester.pumpWidget(
3813+
Directionality(
3814+
textDirection: TextDirection.ltr,
3815+
child: MediaQuery(
3816+
data: const MediaQueryData(),
3817+
child: MouseRegion(
3818+
cursor: SystemMouseCursors.text,
3819+
child: RawScrollbar(
3820+
controller: controller,
3821+
thumbVisibility: true,
3822+
mouseCursor: const WidgetStatePropertyAll<MouseCursor?>(SystemMouseCursors.basic),
3823+
child: SingleChildScrollView(
3824+
controller: controller,
3825+
child: const SizedBox(width: 4000.0, height: 4000.0),
3826+
),
3827+
),
3828+
),
3829+
),
3830+
),
3831+
);
3832+
3833+
final TestGesture gesture = await tester.createGesture(
3834+
kind: ui.PointerDeviceKind.mouse,
3835+
pointer: 1,
3836+
);
3837+
addTearDown(gesture.removePointer);
3838+
3839+
// Pointer enters over the scrollable content (not over the scrollbar):
3840+
// the override should not apply, leaving the parent's I-beam cursor.
3841+
await gesture.addPointer(location: const Offset(100.0, 100.0));
3842+
await tester.pump();
3843+
expect(
3844+
RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1),
3845+
SystemMouseCursors.text,
3846+
);
3847+
3848+
// Move over the painted scrollbar thumb (right edge of the viewport).
3849+
await gesture.moveTo(const Offset(796.0, 5.0));
3850+
await tester.pump();
3851+
expect(
3852+
RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1),
3853+
SystemMouseCursors.basic,
3854+
);
3855+
3856+
// Move back to the content area: the override should yield again.
3857+
await gesture.moveTo(const Offset(100.0, 100.0));
3858+
await tester.pump();
3859+
expect(
3860+
RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1),
3861+
SystemMouseCursors.text,
3862+
);
3863+
},
3864+
);
3865+
3866+
testWidgets(
3867+
'RawScrollbar without mouseCursor preserves the ancestor cursor over the scrollbar',
3868+
(WidgetTester tester) async {
3869+
final controller = ScrollController();
3870+
addTearDown(controller.dispose);
3871+
3872+
await tester.pumpWidget(
3873+
Directionality(
3874+
textDirection: TextDirection.ltr,
3875+
child: MediaQuery(
3876+
data: const MediaQueryData(),
3877+
child: MouseRegion(
3878+
cursor: SystemMouseCursors.text,
3879+
child: RawScrollbar(
3880+
controller: controller,
3881+
thumbVisibility: true,
3882+
child: SingleChildScrollView(
3883+
controller: controller,
3884+
child: const SizedBox(width: 4000.0, height: 4000.0),
3885+
),
3886+
),
3887+
),
3888+
),
3889+
),
3890+
);
3891+
3892+
final TestGesture gesture = await tester.createGesture(
3893+
kind: ui.PointerDeviceKind.mouse,
3894+
pointer: 1,
3895+
);
3896+
addTearDown(gesture.removePointer);
3897+
await gesture.addPointer(location: const Offset(796.0, 5.0));
3898+
await tester.pump();
3899+
// No override: the ancestor's I-beam cursor wins, even over the scrollbar.
3900+
expect(
3901+
RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1),
3902+
SystemMouseCursors.text,
3903+
);
3904+
},
3905+
);
3906+
3907+
testWidgets('RawScrollbar.mouseCursor updates when the property changes while hovered', (
3908+
WidgetTester tester,
3909+
) async {
3910+
final controller = ScrollController();
3911+
addTearDown(controller.dispose);
3912+
3913+
Widget buildFrame(MouseCursor cursor) {
3914+
return Directionality(
3915+
textDirection: TextDirection.ltr,
3916+
child: MediaQuery(
3917+
data: const MediaQueryData(),
3918+
child: RawScrollbar(
3919+
controller: controller,
3920+
thumbVisibility: true,
3921+
mouseCursor: WidgetStatePropertyAll<MouseCursor?>(cursor),
3922+
child: SingleChildScrollView(
3923+
controller: controller,
3924+
child: const SizedBox(width: 4000.0, height: 4000.0),
3925+
),
3926+
),
3927+
),
3928+
);
3929+
}
3930+
3931+
await tester.pumpWidget(buildFrame(SystemMouseCursors.basic));
3932+
3933+
final TestGesture gesture = await tester.createGesture(
3934+
kind: ui.PointerDeviceKind.mouse,
3935+
pointer: 1,
3936+
);
3937+
addTearDown(gesture.removePointer);
3938+
// Add the pointer outside the scrollbar, then hover over it so that the
3939+
// scrollbar registers the hover and applies its cursor override.
3940+
await gesture.addPointer(location: const Offset(100.0, 100.0));
3941+
await gesture.moveTo(const Offset(796.0, 5.0));
3942+
await tester.pump();
3943+
expect(
3944+
RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1),
3945+
SystemMouseCursors.basic,
3946+
);
3947+
3948+
await tester.pumpWidget(buildFrame(SystemMouseCursors.grab));
3949+
// Nudge the pointer so the [MouseTracker] re-reads the cursor for the
3950+
// updated [MouseRegion]; the framework only re-evaluates the active
3951+
// cursor when device position or annotations change.
3952+
await gesture.moveBy(const Offset(0.0, 1.0));
3953+
await tester.pump();
3954+
expect(
3955+
RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1),
3956+
SystemMouseCursors.grab,
3957+
);
3958+
});
38043959
}

0 commit comments

Comments
 (0)