Widgets like PreferredSize or SliverAppBar.expandedHeight need an actual height number to work, but sometimes I want that height to be the height of another Widget that hasn't been rendered yet, so it's height is unknown.
I would like then to simulate the Flutter Framework itself and falsely render a copy of that widget, so I could grab it's size and them use it on the first time my build function runs.
How can this be done?
Related
Is there any method/function which takes widget as input and returns its numerical height without building it.
Or even if it builds the widget but widget is not shown then it's fine.
There is this post:
How to get height of a Widget?
I can't seem to figure out how it works, so any example which still works in latest flutter would be very helpful.
My current condtion is described in flutter : Create scrollable organic gridview with dynamic height
I've solved most of it only dynamic height problem remains.
What is the proper way to handle device size globally. The idea is not to have a [MediaQuery.of(context).size.width] on each screen of the app. There is already a question and answer about it, but the only answer is out of date because there was no null safety yet.
The answer suggests creating a constants.dart file, like in the image below:
1
And initialize in the build of the first widget of the application:
2
The problem is that for it to be constant it must have a value, and not wait for the first build. It is also true that the value canchange based on device orientation and I would like to handle this as well.
I have doubts about it. if someone can help me
You cannot save the screen dimensions as a constant, because they will change if the device is rotated, or when the screen is resized, such as for desktop and web apps.
Instead you should be pushing your cutpoint decisions as low as possible, using LayoutBuilder.
LayoutBuilder seems preferable over every use of MediaQuery for sizing a viewport (either the whole screen, or the space left in a column or other layout).
LayoutBuilder also works hard to avoid rebuilding its child if the size doesn't change and the parents haven't had to re-layout.
The builder function is called in the following situations:
The first time the widget is laid out.
When the parent widget passes different layout constraints.
When the parent widget updates this widget.
When the dependencies that the builder function subscribes to change.
The builder function is not called during layout if the parent passes the same constraints repeatedly.
And you don't have to think about "the height of the appbar" ever again, because you're getting the space left, not the total space on the screen.
Check it out: https://api.flutter.dev/flutter/widgets/LayoutBuilder-class.html
I'd like to add a custom showMore widget if the skills widget which has a dynamic size(width) exceeds the screen width. On clicking the showMore widget it should show all the skills in a wrap .else show less.
In flutter
Constraints go down and sizes go up
See this documentation.
Flutter uses a single pass algorithm to render your application. This is a technical choice to ensure performance but it comes with limitations.
One of them is that, when you are building the widget tree, you only have access to the constraints of the parent, and not any size of any widget (since they are not rendered yet).
So a short answer to your question is:
No, you cannot do what you are trying to do (displaying something if some widgets are not fitting on the screen) since you don't have access to any sizes in the build method.
An alternative solution would be to use Wrap to wrap your chips or use a ListView on the horizontal axis to make the list of chips horizontally scrollable.
Anyway, if you really want to do this, you can hardcode the sizes of your chip and access the device size with MediaQuery.of(context).size or by using the LayoutBuilder and using contraints.maxWidth as the parent's width. Then you can check whether or not numberOfChips * chipSize <= maxWidth. But I wouldn't recommend it as the design wouldn't be responsive:
All the chips will have the same size, so you'll end up with a big chip for "c" and maybe a long name like "python" won't fit in and you'll end up with overflow issues.
What if the user changes the font size of his device? You will also end up with overflow issues.
How can I get the height of a flutter widget? I think it's using key, but I don't know how.
For example I want to get the height of a container, which will be in a stack to be able to put the padding of a list view and that at the initial moment it is under the container.
If you’re wanting to get the height before the children widget are built, use a LayoutBuilder widget to wrap the descendants.
Otherwise you can also check the dev tools by using the Select Wifget tool and looking at the widget”s properties
I'd like to fit multiple widgets into PageView,
if the page is full, I want to move to the next page.
So I'd like to get sizes of the widgets inside the page without building the widgets.
This post seems that it's required to build the widget first before getting the size.
For text, I can do it using RenderParagraph.
Is it possible to get the size of custom widgets?
The widgets I want to get the size are basically text which we can tab each word.