Proper use of MediaQuery in Flutter - dart

What is the best practice for using MediaQuery? The app will be used in different screen sizes, so hard-coding the width and height of a widget might be a problem.
I'm having a hard time understanding the different uses of the MediaQuery class. I tried searching for it online, but I didn't find many examples. It would be great if anyone can help me to understand this class or can provide a link that I can use to understand this MediaQuery class.

A few points there will be:
For instance, MediaQuery.of(context).size.width - will not give you the exact width in pixels for your physical device measurements but rather in logical pixels. To get the exact physical device measurements you need to do something like this: MediaQuery.of(context).size.width * MediaQuery.of(context).devicePixelRatio
MediaQuery.of(context).padding.top is the height of the status bar
kToolbarHeight is the height of the AppBar
in case you have a BottomNavigation bar, the height is kBottomNavigationBarHeight.
So, to deduct all the values above, we can do like this: MediaQuery.of(context).size.height — MediaQuery.of(context).padding.top — kToolbarHeight — kBottomNavigationBarHeight
I've put this tutorial together some time ago, if you are still looking for an answer to your question. Widgets sizes relative to screen size in Flutter using MediaQuery. I hope it helps!

Related

Setting widthUnit/heightUnit for a Sizeable object in Vaadin

For a sizeable object (https://vaadin.com/api/framework/7.7.3/com/vaadin/server/Sizeable.html), we obtain the width and height using getWidth() and getHeight() respectively, and the unit via getWidthUnit() and getHeightUnit(). How do I set the width/height unit?
EDIT:
So, the reason I ask is because I have a Panel and I'm working on a function that automatically scrolls it to a certain line. The Scrollable interface only provides setScrollTop in terms of pixels, so I would want to do something like:
panel.setScrollTop(lineNumber/totalLines * heightOfPanelInPixels)
For that, I'm planning on using the SizeReporter addon to give me "heightOfPanelInPixels", but I'm not sure I can guarantee the height being in terms of pixels. If it's not, I would need some way to convert the units.
Also, I asked about setting the unit since I thought it was just a universal thing: like if I could just set the unit for all Sizeables to report in, say, inches or pixels
There isn't really any way of achieving what you want to do, short of setting all sizes as px values from the server. For any other kind of size definition, the actual size in pixels will vary depending on lots of different factors that only the browser keeps track of. Furthermore, there isn't any mechanism that would pass back the actual sizes to the server as they are resolved.
You might want try this add-on that makes the size of selected components available on the server: https://vaadin.com/directory/component/sizereporter.
Old answer below:
There are two ways of setting the size in either direction. They both lead to exactly the same end result - it's just two different ways of expressing the same intent.
Taking the height as an example, there's the setHeight(String) method that expects a CSS definition such as 20px or 3.5em. This method is convenient to use of you want to set a hardcoded size directly from code.
The other approach splits up the size into a numerical size and a separate unit: setHeight(float, Unit), e.g. setHeight(20, Unit.PX). This method is more practical if you want to do calculations with the size, e.g. doubling it by using setHeight(2 * getHeight(), getHeightUnit()).
Setting and getting widths also work in exactly the same way.

What precisely is gamut in size classes?

When you want to add a variation to an attribute, you get this popup (I'm using Xcode 8) :
As far as I understand, gamut setting here is for a display type. But I don't understand what it really implies and when I should use it ?
You are correct it is for display types, specifically, for having content tailored for say, screens with P3 Gamut (wide color) like the iPadPro (and probably the new iPhone)
https://webkit.org/blog-files/color-gamut/
https://en.wikipedia.org/wiki/DCI-P3
I think you would mostly use it for images and video, if you have content made for the wide gamut.
I found this, http://asciiwwdc.com/2016/sessions/222 , search for gamut and you can get some of the intent.

Change Size of Game in XNA

I don't know a better way to say this, but I'm not looking to change the size of the window. I'm creating a maze, whose size that can be changed via scripting. As such, that may make the maze bigger what the window shows (even on full screen. Is there a way to shrink/enlarge the actual game inside the window?
Well, what you're asking is generally just a bad user experience, cause the size of the game will change, if the maze changes size.
That being said, what you're asking is technically possible. The way to do it would be to use a matrix for SpriteBatch.Begin's last parameter.
This matrix would look something like
Matrix.CreateScale(windowWidth / gameContentTotalWidth, windowHeight / gameContentTotalHeight, 1);
This will scale your game content to always be drawn within the screen. However this means if you make a large maze, you're likely to end up being unable to navigate it, cause you'd be having troubles seeing where you're going.

Autolayout - practical layout of complex screens and a conceptual hole

I'm trying hard to drink the Autolayout KoolAid in Xcode 6. I understand the fundamentals. It's solving a set of linear equations for the coordinates and sizes for all on-screen objects. I've watch the videos and read the tutorials. I've gotten it to work.
With all the different devices and screen sizes out there, it seems like a great solution.
So here's a mock-up of my screen:
When I create this simple layout of 5 labels (just the yellow ones for now), I need 20 constraints, right? Height, width, X position and Y position - for each one.
So my constraint list ends up looking like this:
First, I don't see the what that sort order is there, which makes it hard to find things. Worse, that's already a massive number of constraints to look through to find problems.
This can't be a very complex screen in the big scheme of things. What concept am I missing here? What organizational method would help me? If people are doing large screens with many elements, how are they managing the dozens and dozens of constraints?
Note: I don't want to use a table because that seems like overkill - I don't need to scroll, I don't need buttons in cells, and I don't really want to bother styling a table and cells to look like what I've got there in the mock up. This is really just an example of 5 elements on a screen (15 on that screen, actually).

Do I still need to pad images in a CSS Sprite?

In CSS Sprites you will often find padding between each image. I believe the idea is so that if the page is resized then one image won't bleed into another.
I think this depends on the different types of browser zoom (best explained by Jeff).
However, I haven't been able to see this behaviour in my tests. Is this only a problem with older browsers? (I havent been able to test with IE6 at the current time so I'm counting that as 'old').
Should I still worry about leaving space? Its kind of a pain.
For instance :
A CSS Sprite I found for AOL has
padding between each image : VIEW
but The Daily Show decided not to
bother : VIEW
It shouldn't need to be padded, but when zoomed, especially in IE8 (betas more than the RC), there is image bleeding if there is no padding.
Best example is to go to Google.com -> Search, and zoom... you'll start to see "underlines" at the bottom right of the image as the zooming rounds up/down.
In theory, a 1px padding on all sides of a sprite should be fine.
Here's the sprite from Google (images)...
But when zoomed, the +,-,x icons bleed into the main Google logo.
Basically the answer is yes. Two years to the day after I asked this question will see the release of IE9. IE9 has this problem just as much - if not more than any other browser...
It's pretty infuriating because it's such a simple thing to fix.
With iPads increasing in marketshare - its's pretty essential to at least have a half decent experience with zooming un-uniform amounts.
I am going to have to put a single pixel border around every image to match the background color of the adjacent element (potentially different on each side). Fortunately I auto-generate all my csssprites based on an .xml file - so I can do this programatically without too much hastle. It's still a huge pain though...
Simon - My experience is that this is certainly still a problem.
In response to your second question, why not use transparent padding? (Perhaps you are still supporting ie6 and this is non-trivial, in which case, I'm really sorry).
Speaking of the older browsers (those using text zoom), you don't always need padding.
The main difference between your two examples is that the Daily Show sprite already includes the menu item's text in the image itself.
When using text zoom, the AOL menu items could stretch out vertically due to the larger font size, and the menu text might even wrap to two lines. To accommodate for such eventualities, those icons need a little padding to ensure they don't bleed. Typically, you'd just try to make sure it doesn't bleed on any of IE6's five text sizes.
Since The Daily Show's menu doesn't contain any (visible) HTML text its size won't be affected by text zoom (though you might need a line-height: 0; or so to be sure), so it doesn't need any padding.
As scunliffe already showed, browsers using page zoom may need sprites to have a little padding due to rounding errors.

Resources