Yonkee
Yonkee

Reputation: 1883

How to determine if one offset contained with another offset area?

I would like to determine if a widget is touching another widget, so far I have the following offset - Positions of widgets. However, this is a single point rather the a contained area.

Offset(48.8, 425.6) <- Box 1 Offset(70.0, 456.0) <-- Box 2

What is the best way to determine if Box 1 is near box 2 ( Box 2 would be scaled out by xx number to improve the matching )

I am using GlobalKeys to determine position like below

RenderBox box1 = box1Key.currentContext.findRenderObject();
Offset box1Pos = box1.localToGlobal(Offset.zero);

RenderBox box2 = box2Key.currentContext.findRenderObject();
Offset box2Pos = box2.localToGlobal(Offset.zero);

Upvotes: 0

Views: 1369

Answers (1)

Martyns
Martyns

Reputation: 3885

You could use the class Rect to define the boundings of one of your widgets and then use the method contains

https://api.flutter.dev/flutter/dart-ui/Rect/contains.html

The Rect class also has the method intersect which can be better suited for your question.

https://api.flutter.dev/flutter/dart-ui/Rect/intersect.html

Upvotes: 2

Related Questions