In flutter, to change a mouse cursor we must use the MouseRegion and a setState:
MouseRegion(cursor: _cursor);
setState(() {
_cursor = SystemMouseCursors.grab;
});
But, the setState
is rebuilding the widgets. It sounds crazy to rebuild all the widgets for changing the cursor (which is not related to my user interface, it is the system mouse cursor). In my current application, changing the cursor at each mouse movement trigger a rebuild, and so it is visually flashing ! For example I have a table that reload its data !
I have search for another way to handle this. I tried with MouseTracker
but we have no more access to the MouseAnnotation
: https://docs.flutter.dev/release/breaking-changes/mouse-tracker-no-longer-attaches-annotations
So, I have no idea how to track and change the mouse cursor with a rebuild. And you ?
2
Answers
I use a Stack and I use the property
MouseRegion.opaque
that way. I use the stack to put the mouse region at the top z-order, and so when it rebuilt, nothing else is rebuilt:Where
_mouseCursor
is a ValueNotifier<MouseCursor>
to trigger refreshs.You don’t need the
setState
at all, remove it.to make the cursor change when hovering above a certain widget, just warp it with the
MouseRegion
and change the cursor, that’s it.an example:
}