I'm creating a custom widget for a segment control.
return Padding(
padding: const EdgeInsets.symmetric(vertical: 20.0),
child: Container(
decoration: BoxDecoration(border: Border.all(color: Colors.blue)),
child: Row(
mainAxisSize: MainAxisSize.min,
children: buttons,
),
),
);
I want the container to shrink to the minimum size to contain its children. However when I include the widget in a parent, it expands to fill the parent. This is visible because the border from the decoration is larger than the buttons.
How do I force the container to shrink?
Container is a Single-child layout widget. But it can have multiple children by using a Multi-child layout widget as its child.
Flutter – Change Container Border's Color & Width To change the color and width of Container's border, use its decoration property. Set decoration property with BoxDecoration() object. Set the border property of BoxDecoration() object, with required color and width as per your applications specifications.
Column is a layout widget in flutter which aligns its children vertically. It can have multiple child widgets.
To get the container not to take up the entire parent, you need to tell it where it should be placed within the parent - by default, it doesn't know where to go so it fills the parent =D.
The simplest ways of doing this are to use an Align
or a Center
widget. I believe you want to put it around your Container
in this case, but I'm not 100% sure.
wrapping a Container in a Row() widget makes the container shrinks to the Container's child size as mentioned here,, https://github.com/flutter/flutter/issues/4949
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
child:button()
),
]
),
bit late to the party... I'm still new to flutter so go easy on me, but I think using Align
or Center
as the Parent
and then set the Width
and Height
values for the Child
then the parent should shrink to the child. =D
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With