MaterialStateMouseCursor typedef

  1. @Deprecated('Use WidgetStateMouseCursor instead. ' 'Moved to the Widgets layer to make code available outside of Material. ' 'This feature was deprecated after v3.19.0-0.3.pre.')
MaterialStateMouseCursor = WidgetStateMouseCursor

Defines a MouseCursor whose value depends on a set of MaterialStates which represent the interactive state of a component.

This kind of MouseCursor is useful when the set of interactive actions a widget supports varies with its state. For example, a mouse pointer hovering over a disabled ListTile should not display SystemMouseCursors.click, since a disabled list tile doesn't respond to mouse clicks. ListTile's default mouse cursor is a MaterialStateMouseCursor.clickable, which resolves to SystemMouseCursors.basic when the button is disabled.

To use a MaterialStateMouseCursor, you should create a subclass of MaterialStateMouseCursor and implement the abstract resolve method.

This example defines a mouse cursor that resolves to SystemMouseCursors.forbidden when its widget is disabled.

To see it in action, copy and run this code snippet on DartPad.

import 'package:material_ui/material_ui.dart';

/// Flutter code sample for [WidgetStateMouseCursor].

void main() => runApp(const MaterialStateMouseCursorExampleApp());

class MaterialStateMouseCursorExampleApp extends StatelessWidget {
  const MaterialStateMouseCursorExampleApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('MaterialStateMouseCursor Sample')),
        body: const Center(
          child: MaterialStateMouseCursorExample(
            // TRY THIS: Switch to get a different mouse cursor while hovering ListTile.
            enabled: false,
          ),
        ),
      ),
    );
  }
}

class ListTileCursor extends WidgetStateMouseCursor {
  const ListTileCursor();

  @override
  MouseCursor resolve(Set<WidgetState> states) {
    if (states.contains(WidgetState.disabled)) {
      return SystemMouseCursors.forbidden;
    }
    return SystemMouseCursors.click;
  }

  @override
  String get debugDescription => 'ListTileCursor()';
}

class MaterialStateMouseCursorExample extends StatelessWidget {
  const MaterialStateMouseCursorExample({required this.enabled, super.key});

  final bool enabled;

  @override
  Widget build(BuildContext context) {
    return ListTile(
      title: const Text('ListTile'),
      enabled: enabled,
      onTap: () {},
      mouseCursor: const ListTileCursor(),
    );
  }
}

This class should only be used for parameters which are documented to take MaterialStateMouseCursor, otherwise only the default state will be used.

See also:

Implementation

// TODO(framework): Replace the following block with a @dartpad directive
// when it's supported. https://github.com/dart-lang/dartdoc/issues/4123
/// {@macro material_ui.dartpad_guide}
///
/// {@example /example/lib/material_state/material_state_mouse_cursor.0.dart#body}
///
/// </callout-box>
///
/// This class should only be used for parameters which are documented to take
/// [MaterialStateMouseCursor], otherwise only the default state will be used.
///
/// See also:
///
///  * [WidgetStateMouseCursor], the non-Material version that can be used
///    interchangeably with `MaterialStateMouseCursor`.
///  * [MouseCursor] for introduction on the mouse cursor system.
///  * [SystemMouseCursors], which defines cursors that are supported by
///    native platforms.
@Deprecated(
  'Use WidgetStateMouseCursor instead. '
  'Moved to the Widgets layer to make code available outside of Material. '
  'This feature was deprecated after v3.19.0-0.3.pre.',
)
typedef MaterialStateMouseCursor = WidgetStateMouseCursor;