'flutter get mouse position in desktop and web

How to get mouse coordinates in flutter_desktop so that when the user hovers a specific widget it shows a Container widget with image and text in the the mouse position not a fixed position



Solution 1:[1]

Wrap your widget with MouseRegion to detect when a mouse enters, exits, or hovers over it. You can even change what the cursor looks like to indicate that your user can take an action (or not).

https://www.youtube.com/watch?v=1oF3pI5umck

Solution 2:[2]

You can use MouseRegion to detect mouse cursor movement within the Widget. A sample is provided in the docs, showing that MouseRegion can detect cursor position in its children.

int _enterCounter = 0;
int _exitCounter = 0;
double x = 0.0;
double y = 0.0;

void _incrementEnter(PointerEvent details) {
  setState(() {
    _enterCounter++;
  });
}

void _incrementExit(PointerEvent details) {
  setState(() {
    _exitCounter++;
  });
}

// fetches mouse pointer location
void _updateLocation(PointerEvent details) {
  setState(() {
    x = details.position.dx;
    y = details.position.dy;
  });
}

...

// Use this as a Widget
MouseRegion(
  onEnter: _incrementEnter,
  onHover: _updateLocation,
  onExit: _incrementExit,
  child: Container()
)

Demo

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abhimanyu Kharwal
Solution 2 Omatt