[Flutter/Dart] How to change icon of grabbing and ungrabbing.

Dart

Preparation

  • You have to use StatefulWidget or HookWidget
  • You can implement it without any event conflicts by using Listener widget.

SourceCode

I will intraduce it by using flutter_fooks

Define State

Define cursorIcon state inside a build method.

    final cursorIcon = useState<SystemMouseCursor>(SystemMouseCursors.grab);

Wrap your widget by Listener

  1. Wrap your widget by Listener.
  2. Also wrap your widget by MouseRegion.
    child: Listener(
    onPointerDown: (_){
        debugPrint("down");
        cursorIcon.value = SystemMouseCursors.grabbing;
    },
    onPointerUp: (_){
        debugPrint("up");
    cursorIcon.value = SystemMouseCursors.grab;
    },
    child: MouseRegion(
    cursor: cursorIcon.value,

If you want to use another cursor icons , you can change SystemMouseCursors.XXXX.

コメント

タイトルとURLをコピーしました