Fixed mouse cursor of disabled IconButton (#84946)

This commit is contained in:
Viren Khatri 2021-08-11 05:02:40 +05:30 committed by GitHub
parent 1ca0333ce1
commit 66597ffb78
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 72 additions and 4 deletions

View file

@ -153,7 +153,7 @@ class IconButton extends StatelessWidget {
this.splashColor,
this.disabledColor,
required this.onPressed,
this.mouseCursor = SystemMouseCursors.click,
this.mouseCursor,
this.focusNode,
this.autofocus = false,
this.tooltip,
@ -280,8 +280,10 @@ class IconButton extends StatelessWidget {
/// {@macro flutter.material.RawMaterialButton.mouseCursor}
///
/// Defaults to [SystemMouseCursors.click].
final MouseCursor mouseCursor;
/// If set to null, will default to
/// - [SystemMouseCursors.forbidden], if [onPressed] is null
/// - [SystemMouseCursors.click], otherwise
final MouseCursor? mouseCursor;
/// {@macro flutter.widgets.Focus.focusNode}
final FocusNode? focusNode;
@ -379,7 +381,7 @@ class IconButton extends StatelessWidget {
autofocus: autofocus,
canRequestFocus: onPressed != null,
onTap: onPressed,
mouseCursor: mouseCursor,
mouseCursor: mouseCursor ?? (onPressed == null ? SystemMouseCursors.forbidden : SystemMouseCursors.click),
enableFeedback: enableFeedback,
focusColor: focusColor ?? theme.focusColor,
hoverColor: hoverColor ?? theme.hoverColor,

View file

@ -680,6 +680,72 @@ void main() {
expect(RendererBinding.instance!.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.click);
});
testWidgets('disabled IconButton has forbidden mouse cursor', (WidgetTester tester) async {
await tester.pumpWidget(
const Material(
child: Directionality(
textDirection: TextDirection.ltr,
child: Center(
child: IconButton(
onPressed: null, // null value indicates IconButton is disabled
icon: Icon(Icons.play_arrow),
),
),
),
),
);
final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse, pointer: 1);
await gesture.addPointer(location: tester.getCenter(find.byType(IconButton)));
addTearDown(gesture.removePointer);
await tester.pump();
expect(RendererBinding.instance!.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.forbidden);
});
testWidgets('IconButton.mouseCursor overrides implicit setting of mouse cursor', (WidgetTester tester) async {
await tester.pumpWidget(
const Material(
child: Directionality(
textDirection: TextDirection.ltr,
child: Center(
child: IconButton(
onPressed: null,
mouseCursor: SystemMouseCursors.none,
icon: Icon(Icons.play_arrow),
),
),
),
),
);
final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse, pointer: 1);
await gesture.addPointer(location: tester.getCenter(find.byType(IconButton)));
addTearDown(gesture.removePointer);
await tester.pump();
expect(RendererBinding.instance!.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.none);
await tester.pumpWidget(
Material(
child: Directionality(
textDirection: TextDirection.ltr,
child: Center(
child: IconButton(
onPressed: () {},
mouseCursor: SystemMouseCursors.none,
icon: const Icon(Icons.play_arrow),
),
),
),
),
);
expect(RendererBinding.instance!.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.none);
});
}
Widget wrap({ required Widget child }) {