Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter container inside a container

Tags:

flutter

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?

like image 754
osamu Avatar asked Sep 04 '18 01:09

osamu


People also ask

Can I put container inside container in Flutter?

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.

How do you use the container container in Flutter?

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 do you add icons inside a container in Flutter?

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.

Can container have children Flutter?

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.


1 Answers

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,     ),   ), 
like image 182
Richard Heap Avatar answered Sep 16 '22 15:09

Richard Heap