I'm practicing with flutter Container these days, and this example I came up with is simply astonishing
I put a Container of size 50x50 inside a Container of size 200x200. Strange is that the inner Container expand to 200x200 even though it has a tight constraint of 50x50.
Here's the code
Container( width: 200.0, height: 200.0, color: Colors.orange, child: Container( width: 50.0, height: 50.0, color: Colors.blue, ), )
I expect a small blue box inside a bigger orange box.
Could someone explain why?
To size a Container that resides inside another Container, you need to set the alignment property of the parent Container. Otherwise, the child Container won't care about the width and height you set for it and will expand to its parent's constraints.
Container class in flutter is a convenience widget that combines common painting, positioning, and sizing of widgets. A Container class can be used to store one or more widgets and position them on the screen according to our convenience. Basically, a container is like a box to store contents.
How to Add Icon in Flutter App? You can use Icon() widget to add icons to your Flutter App. You have to pass the icon data as an icon to this widget. You can use default available Material icons with Icons class.
Theoretically, it is possible. A child widget can have a list of children. In that case, a container will size itself to the size of the incoming children. Otherwise, it tries to be as small as possible.
You need to specify where in the orange box you would like the blue box displayed, otherwise the blue box will grow to the size of its parent.
Container( width: 200.0, height: 200.0, color: Colors.orange, alignment: Alignment.center, // where to position the child child: Container( width: 50.0, height: 50.0, color: Colors.blue, ), ),
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