i'm trying to set widgets sizes according to screen height. For example i have 3 widgets inside my body, the first and second one should be 20% from whole screen size, the third one - the rest.
The problem is that i have CupertinoSliverNavigatoinBar and CupertinoTabBar, so i can't calculate height of my third widget.
I'm trying to implement this thing.
According to documentation, CupertinoTabBar height is 50.0. To verify this you can check the source code of CupertinoTabBar and there is constant which called _kTabBarHeight.
But for your particular problem I think you should use Expanded widget with flex property.
Related
Is there some widget that makes it easy to swap out one child in a Row for another smaller one in case the width is not enough to fit the bigger one?
In the image below, the Widget B is a full width version, and D is a mini version of the same widget.
Note that I do not want the widget to adjust its size seamlessly like a Flexible or Expanded - I want it to be either as wide as B, or if that doesn't fit I want it to be as wide as D - not anything in between.
I know I can do this using a LayoutBuilder and a hardcoded condition to check if the available width is at least X pixels. But is there a widget out there that does it without needing a hardcoded min-width? Just adapting to the actual width of B?
You can use a stateful widget and use addPostFrameCallback to get the size from the context:
WidgetsBinding.instance?.addPostFrameCallback((_) {
if (context.size is Size) {
print('My context size is: ${context.size}');
}
});
If you want to know the sizes of both widgets, you can assign a global key to each, and then use a RenderBox to get their sizes.
Any solution that you go with to dynamically get the size of widgets will necessarily mean that they will get laid out, which may not be cheap, depending on the widgets themselves.
My Flutter UI has a Row widget with and arbitrary number Widgets in it. I would like to move all of those widgets over to the right by a fixed amount. But the caveat is, if the other widgets grow in width such that the available horizontal space is consumed, the spacing widget will relinquish its space.
The Spacer widget does not work for me, as it does not allow you to specify a fix maximum. It only allows a flex value, which is a function of the width of the other content in the row. I want this spacer to take up a fixed amount of space regardless of the width of the other content of the row (unless all the room is used up).
Try using sizedBox or FractionallySizedBox as explained in this answer
https://stackoverflow.com/a/63430801/8213343
In a flutter app, what is the difference between a Container inside a Row and a Row inside Container?
In flutter in each time I want to make an app, I took minutes to ask my self
which one should I choose?
In flutter Row and Column provide a way to use the Flex layout system aka FlexBox.
In short Flex is a one-dimensional layout system, where in the children can be aligned on the main-axis or cross-axis, the main axis can be defined as horizontal or vertical.
A Container in Flutter is a general-purpose widget that gives you access to a range of properties like padding, decoration, size etc. A full-list is available at the API Docs
To answer your question,
A Container in a Row is like we said above, a child of a Flex, which means it will respect the properties of alignment set by the parent (MainAxisAlignment/CrossAxisAlignment )
A Row in a Container, is the opposite of that which means that, the Row or Column will respect properties set on the Container that is for example the Decoration or Size properties amongst others.
Flutter is a fairly new framework as of today and there are many setbacks when it comes to flexible design. What I want to achieve can be demonstrated easier by images.
As you see when the tiles have a border-radius such as in the images they have to overlap in order not to leave empty space like in the first image. I achieved this using the 'Stack' widget of course by giving fixed height for the widget above and fixed top padding for the following widget in a loop so that I got the view in the 2nd and 3rd images. However, this approach prevents me from using dynamic sizes within each widget because I cannot get the height of the previous widget in the build-time.
Has anyone faced this kind of UI pattern or a similar one that can help me with this case? I appreciate any collaboration on this.
I think you need to calculate things on your own, not taking into consideration how it was painted but precisly how you want it to be painted with math.
Of course to find dynamic values there is this to use inside your build function:
var screenSize = MediaQuery.of(context).size;
var width = screenSize.width;
var height = screenSize.height;
and based on this you can make your calculations.
If you need to have those values before build functions runs, for your initState or whataver reasons you can initialize this widget with width and height if you know what I mean, since width and height of the screen is equal in everywidget.
I have a ListView with a Column, and inside that Column 2 buttons.
The ListView is stretched to the entire page (it contains more elements)
I want all the buttons inside the Column to stretch to the button with the largest width.
I found a solution on usnig InstrictWidth, but when used inside a Listview I'm getting the error
LayoutBuilder does not support returning intrinsic dimensions.
How can I still achieve what I'm looking for?
Wrap each button with an Expanded widget and then wrap each one of these Expanded widgets with a Row widget