Re: Screen/Widget size

547 views
Skip to first unread message

Adam Barth

unread,
Apr 24, 2016, 12:27:31 PM4/24/16
to robbi...@gmail.com, Flutter Dev
On Sun, Apr 24, 2016 at 6:21 AM <robbi...@gmail.com> wrote:
Before I file a bug I wanted to run this by the group here to see if I'm missing something obvious.

I'm having trouble getting the correct screen size or the correct size of widgets from the build context.  I'm aware of this thread:
https://groups.google.com/forum/#!searchin/flutter-dev/RootWidget/flutter-dev/s4hUn1-ZFL8/YHRmHp29CQAJ

When I reduce my app to just the CustomPaint suggested in the thread above it reports the size as 360.0 x 592.0.  This is the same as is reported when I do a MediaQuery on widgets at various levels of my widget hierarchy, including the top level route.  And the same as reported when I dump the render tree.

The actual resolution of my device is 1080.0 x 1920.0.  Flutter seems to render everything as expected despite the unexpected size.  The problem is that I need the real size for some of my algorithms and have been unable to find a way to get it.

The Flutter framework works in terms of logical pixels, which have roughly the same size across devices.  If you want to convert to physical device pixels, you can multiply by the devicePixelRatio, which is available via MediaQuery.of:


A - can anyone point out to me what might cause this situation?  Is this a general flutter bug or a bug on certain devices?  Is there something I should be doing to force my app to fit the screen logically as well as visually (docs say the root widget is supposed to do this automatically)?

Yes, the root widget will fill the screen.  It's just that sizes are reported in logical pixels and you'll need to multiply by the device pixel ratio if you want to know how many physical pixels that corresponds to.

B - It'd be great, IMO, for developers if there were streamlined ways to get the screen and or root widget and its context/data  

If you use MaterialApp [1], you can get access to one of the top widgets using Overlay.of [2].  That's useful if you want to add entries to the overlay, which will float those widgets above the rest of the app.

If you want to access the top-level widget in order to determine its size, a better approach is to use MediaQuery.  The media query knows the size of the screen even before any widgets of going through layout.  If you use MediaQuery.of [3], your widget will automagically rebuild if the size of the screen changes (e.g., if the phone rotates).

Adam



Ian Hickson

unread,
Apr 24, 2016, 12:47:51 PM4/24/16
to robbi...@gmail.com, Flutter Dev
On Sun, Apr 24, 2016 at 6:21 AM <robbi...@gmail.com> wrote:
Before I file a bug I wanted to run this by the group here to see if I'm missing something obvious.

I'm having trouble getting the correct screen size or the correct size of widgets from the build context.

In addition to what Adam said, I would add that one key thing to be aware of is that at build time, we don't know anything's size, because build happens before layout. If you are trying to decide what size certain widgets should be at build time, you can use something like a MultiChildCustomLayout widget.

Most of the time, though, you just describe the layout (Rows, Columns, Aligns, Containers, etc) and we figure out the sizes from there (and automatically reflow it if, e.g., you rotate the screen).

We haven't yet done a good job of documenting this, it's a known issue.

-- 
Ian Hickson 
Reply all
Reply to author
Forward
0 new messages