How to get the DOM element with the mouse, indicating the position in the dart script?

How to get DOM element using mouse pointer position in dart script? Does dart have a library for this?

+4
source share
2 answers

Documenthas a method elementFromPointthat should do what you want, but I think this is not so useful in practice, Often you have some elements that hide those that you want to find with the mouse, or you need all the elements under the dot, not only the topmost.

I tend to fold my own logic to find an element, but you have to be careful because the element is not always where it claims. CSS transforms in particular can throw things away.

Here is the basic idea:

Iterable<Element> getElementsAt(Element container, Point p) =>
    container.children.where((c) => containsPoint(c.offset, p));

bool containsPoint(Rectangle r, Point p) =>
    r.left <= p.x && r.right >= p.x && r.top <= p.y && r.bottom >= p.y;
+3

(onMouseDown, onMouseMove ..) MouseEvent. MouseEvent , . :

document.onMouseMove.listen((MouseEvent e) {
  Element target = e.target;
  print(target.id);
});
+2

Source: https://habr.com/ru/post/1527414/


All Articles