Widgets 01 | Container
This tutorial is about how to use the container in the flutter. You learn more detail about Container to use in this tutorial.
Last updated
This tutorial is about how to use the container in the flutter. You learn more detail about Container to use in this tutorial.
Last updated
Container means a parent widget that contain a child widget and manage it, such as width, height, background and so on.
Before use it, we should create page to contain it, we create a ContainPage as a code structure in order to show our code.
You do not need to understand the code above, you just need to know, we will code in the children
of Column
like Text("Hello world")
Then It will be show you like this.
If you want to use a Container
, you can just replace the Text("Hello world")
like below, it just contain a child with default parameters.
It will show you like this. So easy, Ha!
We can simple use now, but we don't know what it really can do, let's look at the constructor of the Container
.
We know it has these parameters, so let's see the detail using of these parameters.
This parameter is used to add a child widget for the container, like the Text("Hello world")
, only if the type of this parameter is a widget, it can be used here.So the example is above, we do not need more explanation for it.
The color is used above, you can see that it change the background color of the text. So we can see that, the color is the background-color of the container. The type is the Color.We can add a Container with green color.
It will show like this.
You should care about the color parameter, it can't be used when there also have the parameter `Decoration decoration`. So the two parameters, you can use only one parameter.
This parameter is used to set its child distance from four directions. We can see its define in the code.
Then we know that we must use the class extend EdgeInsetsGeometry
. So we only have two choices: EdgeInsetsDirectional
and EdgeInsets
in the flutter. EdgeInsetsDirectional
is depends on direction of the TextDirection
, its using is the same as EdgeInsets
. Now, let's focus on the class EdgeInsets
, it just depends on the distance from top,bottom, left and right.
Add the arrow description what is the top, bottom, left, right for padding with EdgeInsets
.
There are also others functions of the EdgeInsets
.You can use them as your try, such as below.
As you can see, it will control its width,height with these two parameters. Just see a example.
This parameter has some similar with the this, they are both about the distance from the edge. But there is some different between two parameters. Let's see the different in the picture.
If we see the define with margin parameter, you can see its type is same as the padding withEdgeInsetsGeometry
. So we can use EdgeInsets
too.
A example.
You can see, we use the different param comply the same distance effect.
We can see the define in the project.
But you do not use it directly , you should use its child class. Let's see the relationship about the AlignmentGeometry
.
The Alignment
has follow constant.
We can use one to the effect, the code means that we show the child at the bottom-right of the Container
.
The same we can use the AlignmentDirectional
to show locate our child widget. Also, we have these constants as following.
This depends on the direction of textDirection
. The using is the same as the Alignment.
We can see the define param, its class is BoxConstaints
.
It has many constructors, such as tight
, loose
and expand
. I will focus on these three constructors, others will remain to you.
This means that if you assign a size to it, which will make a fixed value to it,the min=max. So does not have the change range.
You can see this constructor, the min =0.0 and the max is the assign value. So you give a max value to it if you assign a value like this.
That means you can choose width or height or both to assign. If you assign to it, the param will be a fixed value and min=max. If you do not assign, it will be equal to infinity
, so it will as large as possible in the parent widget. This time, I just show an example with expand
.
In the code above, the minHeight,maxHeight=50.0
,minWidth,maxWidth =double.infinity
. Show below.
In the part, I will tell you two parameters, they have the same type but different effect.
So let's see different between them as below. We can see the foregroundDecoration
cover on the child, and the child is upon the decoration
.
This is main different between them, so I just show you how use the decoration then you can use the foregroundDecoration
as the same way. The Decoration is abstract class, so you can't use it directly. You must use its child class, let's see its extends relation with two main classes.
This parameter is used to change the container coordinate in the parent widget. Because this parameter relate with the math, it will be a bit difficult to understand. So we just get a simple to show the effect, do not go deep with this class.
Then, our container is complete, you do need understand all of them, just know there are some parameters you can use to comply your requirement and choose to use , also you need to take more excise to make better. Good luck!
Thanks for your reading!
The end.
Facebook Page
GitHub
1
2
3
topLeft
topCenter
topRight
centerLeft
center
centerRight
bottomLeft
bottomCenter
bottomRight
1
2
3
topStart
topCenter
topEnd
centerStart
center
centerEnd
bottomStart
bottomCenter
bottomEnd
Facebook Page
QQ Group
GitHub
Flutter Open
NieBin
963828159
NieBin