rrousselGit / flutter_hooks

React hooks for Flutter. Hooks are a new kind of object that manages a Widget life-cycles. They are used to increase code sharing between widgets and as a complete replacement for StatefulWidget.
MIT License
3.11k stars 176 forks source link

Add support for various mouse APIs, `useMouseOver`, 'useMouseOffset', etc #206

Open esDotDev opened 3 years ago

esDotDev commented 3 years ago

Is your feature request related to a problem? Please describe. It's cumbersome to manually create desktop effects for the mouse cursor. We need to either use a StatefulWidget w/ MouseRegion widget 2 event listeners, and an internal _isMouseDown bool, or some combination of nested Build widgets.

Similarly, getting the mouse position is a fairly verbose process of using MouseRegion, some local Offset, and maybe a globalToLocal call on the context.

Describe the solution you'd like bool isMouseOver = useMouseOver(); Offset mousePos = useMouseOffset(); Offset localMousePos = useMouseOffset(local: true);

rrousselGit commented 3 years ago

It's feasible but needs adding support for mounting RenderObjects using hooks.

Doable, but requires a bit of work.

esDotDev commented 3 years ago

Nice! I took a stab at it, but am really out of my depth when trying to use gesture detection manually without a widget. Cant find a clear code example anywhere on how this would actually be setup.

davidmartos96 commented 3 years ago

It's feasible but needs adding support for mounting RenderObjects using hooks.

Doable, but requires a bit of work.

Would such addition provide the possibility to create a hook like useConstraints, the equivalent to a LayoutBuilder? That would be very useful for responsive UI related hooks and extract common logic in the app.

For example a custom hook like: useAvailableSpace(). It could return an enum (narrow, medium, wide) by using the constraints hook under the hood.

rrousselGit commented 3 years ago

LayoutBuilder is special

I don't think it's feasible to implement as a hook. The main issue is, it invokes its builder asynchronously. So hooks wouldn't be able to synchronously get the value