I want to learn more about offset, I googled and found very few tutorials.
I found this tutorial https://blog.logrocket.com/understanding-offsets-flutter/ but is a bit complex.
I learned somewhere that it was expressed from the center of the screen Offset(0,0) (with Offset(dx,dy) and for instance:
- Offset(1,0) was the center right
- Offset(-1,0) was the center left
- Offset(0,1) was the bottom center
- Offset(0,-1) was the top center
but I have other examples with Offset(30,20) …
I am lost, in which unit is it expressed?
Does in depend on the type of widget?
Is there a simple tutorial out there?
Thank you!
2
Answers
Offset
,Alignment
,Rect
, andSize
are all very important to understand. I spend a bit of time reviewing their protocols from time to time, and it pays off when I must deal withPositioned
widgets inside aStack
, or painting on the canvas withCustomPainter
.It scares me to see all these painting apps that deal with x and y as pairs of variables, instead of using the self-paired offsets and math on those. Especially cool is
Offset.fromDirection
which does a polar-to-rect calculation, meaning you don’t have to remember your trig very much to lay out that clock-face.What an
Offset
does depends entirely on theWidget
it’s used in. See this full example:Here we use it in an
Transform.translate
and there the values used in theOffset
equal the number of pixels it will be shifted, so you get this output:If we replace it with an
FractionalTranslation
instead like this:we get this instead while using the same offsets:
Here it shifts according to its own size.
And there’s also
FractionalOffset
s for example. For example:or
Here 0,0 corresponds to the topleft of the parent. 1,1 to bottomleft. And 2,2 will then mean the position so 1,1 is exactly in between etc. So behavior also depends on the the window size.
There’s many more possibilities where in each it could mean something else