How to set the height of a widget dynamically in flutter? - flutter

Container(
height: min(
widget.order.products.length * 20.0 + 10,
100,
),)
widget.order.products.length => will return me the list of dynamic products.
While viewing the list of these products, I want the set the height of the container dynamic which I'm not able to do with the "min" function.
Can anyone help me.
Thanks in advance.

This can be done by using an IntrinsicHeight:
https://api.flutter.dev/flutter/widgets/IntrinsicHeight-class.html
A widget that sizes its child to the child's intrinsic height.
This class is useful, for example, when unlimited height is available and you would like a child that would otherwise attempt to expand infinitely to instead size itself to a more reasonable height.
The constraints that this widget passes to its child will adhere to the parent's constraints, so if the constraints are not large enough to satisfy the child's maximum intrinsic height, then the child will get less height than it otherwise would. Likewise, if the minimum height constraint is larger than the child's maximum intrinsic height, the child will be given more height than it otherwise would.
This van be put Around things like a listView, Column etc.

Related

Make widget always expand to screen border

Is there a way to make a widget, for instance a Container, always expand to a screen border, not dependent of a specific widget tree structure?
Containers impose constraints on their children so you can't make a child's width bigger than its parent when it is a container
some tips :
you can use the Expanded widget to take all the remaining space
to take all the size of the screen you can use width: double.infinity
As they said already, in Container() you can use width: double.infinity or width: MediaQuery.of(context).size.width. But for this, the parent of Container can't have a size smaller than the screen.

What does incoming constraint mean in Flutter?

Containers with no children try to be as big as possible unless the incoming constraints are unbounded, in which case they try to be as small as possible. Containers with children size themselves to their children. The width, height, and constraints arguments to the constructor override this.
Question
What does the incoming constraint mean in Flutter?
According to Google's flutter documentation on understanding constraints:
A widget gets its own constraints from its parent [the "incoming constraint" you are asking about]. A constraint is just a set of 4 doubles: a minimum and maximum width, and a minimum and maximum height.
Then the widget goes through its own list of children. One by one, the widget tells its children what their constraints are (which can be different for each child), and then asks each child what size it wants to be.
Then, the widget positions its children (horizontally in the x axis, and vertically in the y axis), one by one.
And, finally, the widget tells its parent about its own size (within the original constraints, of course).
I suggest you read that entire URL for more details. The Google people are writing some amazingly clear documentation.

GridView which automatically sets row height to the tallest grid item

The title is a simplified version of what I'm trying to achieve: a GridView which automatically sets row height based on a prototype grid item that I supply. The prototype item is a tallest possible item, for example if the grid items contain text, the prototype would contain a longest possible text.
A possible workaround is a ListView in which each row is wrapped in an IntrinsicHeight widget. But:
Row height is not necessarily uniform like in a GridView.
IntrinsicHeight is inefficient per the documentation. Not sure how much though, perhaps it's negligible for simple layouts. Ideally this "compute intrinsic height" operation would only be called for the one prototype row.
For setting custom row heights depending upon the item that is fed into the grid item you can consider using this flutter_staggered_grid_view plugin.
It provides different widgets for both normal grids and sliver grids and will take care of the random size of input. So whatever size of widget you feed to the grid it'll take the size of the item
As far as I understand, you want to dynamically set size for GridView item. There are two ways, calculate it manually and set every time based on your contents, or to use SliverGridDelegateWithMaxCrossAxisExtent. So in your GridView.builder you can add following attribute:
gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent:
200.0, // maximum size of item (on small screens 1 item per row, on bigger as many as can fit with 200.0 px width)
childAspectRatio: 5 / 6,
crossAxisSpacing: 20,
mainAxisSpacing: 20,
),
It gives you access to extra tools for managing the GridView items keeping the aspect ratio, it also has maximum size that it should not exceed

How to properly manage parent:child relationships

Card, by default, assumes the size of its child. So, if we want to change the size of that card, then we also need to change the size of its child.
Text, on the other hand, is a widget. Which, by default, only takes as much space as this text needs. Therefore, if you want to change the size of Text, you need to also change the size of its parent. Since the Card depends on the child and the Text depends on the parent.
Card by default does not have any height or width. The width/height depends on the Child or the Card. In the screenshot, the child here is Text() Widget. The Text widget takes as much space as it's font size. So more the font size is, the larger will be Card.
The last part of the explanation is incorrect. You can't change parent size here as "Card" widget does not has any size properties. So if u want bigger card and small text, use "Container" Widget instead of Card. Almost all developers use Container only to create Card. Container has more feature then a card has.
Container(
height: 100,
width: 100,
child: Material(
elevation: 5,
child: Text("Chart!"),
),
)

What does actutally Bottom overflowed by 123 pixels mean in flutter

I am learning flutter and currently switched from android to flutter.In flutter i mostly get an error something like
bottom overflowed by 234 pixels or renderbox overflowed by 340 pixels.And i fixes that problem by increase the height of the widget.If so then how to know that what much size giving to widget.I mean in android we can declare the height of the layout to be wrap content and its works perfectly.Please explain me that how can i avoid this situation because if i fixes the issue by changing the height of widget in one device then in devices of other screen sizes if throws same error ?Here is a image which throws an error.Ignore the red error , see the error in below screen.Thanks in advance.
!https://imgur.com/a/PsZzeMp
If you want to give fixed width and height to your widgets wrap it with SizedBox.
You can specify fixed width and height and you child widget will be the exact dimension.
If you want one of the dimension fixed and the other as big as the parent widget, you can try something like this:
SizedBox(
width: double.inifinity,
height: 50.0
child: Conttainer()
)
If you are worried about giving fixed dimensions. You can give the height or width according to the ratio of the screen size. You can get the height and width of the screen like this:
double width = MediaQuery.of(context).size.width;
Use the above value to give the width to your widgets like below:
Container(
width: MediaQuery.of(context).size.width * 0.8,
height: 100,
)
To answer question in the comment. You can do the following to wrap the text with dynamic content.
ConstrainedBox(
constraints: BoxConstraints(
maxWidth: 100
),
child: Text(
'ADASFASF ssss'
),
)
The above code will wrap the text to next line if the text widget is more than 100 pixels wide. As we don't have any constraints on the height.
Overflow happens when the minimum size of a child widget is bigger than the parent's constraints (width and/or height).
Which means you can:
make the parent widget bigger (eg: SizedBox(height:, width:,))
make the child smaller (eg: using a FittedBox(fit: BoxFit.scaleDown) widget)
Though the first method will not allow you a size bigger than the parent's parent widget. It should be enough to build the children relatively to their parents and not the other way.
In your case, it seems like you text + image widget is taking a little too much space. I recommend wrapping it in a FittedBox that will scale down the child widget until it fits in you bottom bar.
FittedBox(
fit: BoxFit.scaleDown,
child: _buildChildWidget(),
)
You can then wrap it in other widgets to build the layout you want (Row, Expanded, Flexible, LayoutBuilder, ...).
As the overflow issue usually happens because text or images are too big. A good exercise is trying to make your app work while setting text size to the maximum value allowed by the accessibility options of your device. You can do the same with images by setting an image size relative to your text size for example.
Paddings and margins can also cause problems because they leave less space for the child widget.