From 66597ffb78caa7fb36c682ab98f9b54b9ea1f16a Mon Sep 17 00:00:00 2001 From: Viren Khatri Date: Wed, 11 Aug 2021 05:02:40 +0530 Subject: [PATCH] Fixed mouse cursor of disabled IconButton (#84946) --- .../flutter/lib/src/material/icon_button.dart | 10 +-- .../test/material/icon_button_test.dart | 66 +++++++++++++++++++ 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/packages/flutter/lib/src/material/icon_button.dart b/packages/flutter/lib/src/material/icon_button.dart index 940e7f89b8..4c89d46a31 100644 --- a/packages/flutter/lib/src/material/icon_button.dart +++ b/packages/flutter/lib/src/material/icon_button.dart @@ -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, diff --git a/packages/flutter/test/material/icon_button_test.dart b/packages/flutter/test/material/icon_button_test.dart index 01c4a6f300..3e4daf2fa4 100644 --- a/packages/flutter/test/material/icon_button_test.dart +++ b/packages/flutter/test/material/icon_button_test.dart @@ -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 }) {