What is Container
The container in Flutter is a parent widget that can contain multiple child widgets and manage them efficiently through width, height, padding, background-color any many more we are discussing all the properties with examples.
Generally, it is similar to a box for storing contents. It allows many attributes to the user for decorating its child widgets like margin, padding, and border.
If we have a widget that needs some background styling like color, shape, or size constraints, we may try to wrap it in a container widget. This widget helps us to compose, decorate, and position our child widgets.
From this diagram, you can understand how these container properties are working. Now let's discuss all the properties in detail with examples.
Properties of Container widget:-
1. color
This property is used to set the background color of the text. It also changes the background color of the entire container. You need to set the color property for that as shown in the below example:
Container(
color: Colors.blueGrey,
),
2. child
This property is used to store the child widget of the container. Suppose we have taken a Text widget as its child widget and need to show that text which has some styling and show in the center of the container that can be shown in the below example:
Container(
child: Center(
child: Text(
"What is Container? ",
style: TextStyle(
color: Colors.white, fontSize: 30, fontWeight: FontWeight.bold),
),
)
)
3. height and width:
This property is used to set the container's height and width according to our requirements. By default, the container always takes the space based on its child widget. See the below code:
Container(
height: 400,
width: 400,
color: Colors.blueGrey,
child: Center(
child: Text(
"What is Container ? ",
style: TextStyle(
color: Colors.white, fontSize: 30, fontWeight: FontWeight.bold),
),
),
)
If we want to set the height and width which covers the whole screen then you need to use double.infinity which is shown in the below code :
Container(
height:double.infinity,
width: double.infinity,
color: Colors.blueGrey,
child: Center(
child: Text(
"What is Container ? ",
style: TextStyle(
color: Colors.white, fontSize: 30, fontWeight: FontWeight.bold),
),
),
)
4. margin:
This property is used to give the space outside of the widget. Suppose we need space from four sides of the widgets then we can use the EdgeInsets.all(25) that set the equal margin in all four directions.
You can see in the diagram how the margin is working it will give the space from outside of the WidgetBorder.
Container(
height: 400,
width: 400,
color: Colors.blueGrey,
margin: EdgeInsets.all(20.0),
child: Center(
child: Text(
"What is Container ? ",
style: TextStyle(
color: Colors.white, fontSize: 30, fontWeight: FontWeight.bold),
),
),
)
Also set the margin individualy like EdgeInsets.only(top:25) same for the Bottom, Left and Right Margin as shown in the below example:
Container(
height: 400,
width: 400,
color: Colors.blueGrey,
margin: EdgeInsets.only(left: 15.0,top: 10.0,right: 15.0,bottom: 10.0),
child: Center(
child: Text(
"What is Container ? ",
style: TextStyle(
color: Colors.white, fontSize: 30, fontWeight: FontWeight.bold),
),
),
)
5. padding
This property is used to set the distance between the border of the container including all four directions and its child widget. We can observe this by seeing the space between the container and the child widget. Here, we have used an EdgeInsets.all(35) that set the space between text and all four container directions:
You can see in the diagram how the padding is working it will give the space from inside of the Widget Border.
Container(
height: 400,
width: 400,
color: Colors.blueGrey,
margin: EdgeInsets.all(20.0),
padding: EdgeInsets.all(20.0),
child: Center(
child: Text(
"What is Container ? ",
style: TextStyle(
color: Colors.white, fontSize: 30, fontWeight: FontWeight.bold),
),
),
)
Also set the margin individualy EdgeInsets.only(top:25) same for Bottom, Left and Right Padding as shown in the below example:
Container(
height: 400,
width: 400,
color: Colors.blueGrey,
margin: EdgeInsets.all(20.0),
padding: EdgeInsets.only(left: 15.0,top: 10.0,right: 15.0,bottom: 10.0),
child: Center(
child: Text(
"What is Container ? ",
style: TextStyle(
color: Colors.white, fontSize: 30, fontWeight: FontWeight.bold),
),
),
)
6. alignment
This property is used to set the position of the child within the container. Flutter allows the user to align its element in various ways: center, bottom, bottom center, topLeft, center right, left, right, and many more. In the below example, we are going to align its child into the top right position.
Container(
height: 400,
width: 400,
color: Colors.blueGrey,
alignment: Alignment.topRight,
child: Text(
"What is Container ? ",
style: TextStyle(
color: Colors.white, fontSize: 30, fontWeight: FontWeight.bold),
),
),
7. decoration
This property allows the developer to add decoration to the widget. It decorates or paints the widget behind the child. The BoxDecoration class provides a variety of ways to draw a box.
BoxDecoration provides multiple provide like border, borderRadius, boxShadow, Color, and many more I have given an example that shows the border or container and radius of that border.
Container(
height: 400,
width: 400,
decoration: BoxDecoration(
border: Border.all(color: Colors.orange, width: 5),
borderRadius: BorderRadius.circular(10),
boxShadow: [
new BoxShadow(
color: Colors.redAccent,
offset: Offset(10.0, 10.0),
),
],
),
alignment: Alignment.topRight,
child: Text(
"What is Container ? ",
style: TextStyle(
color: Colors.white, fontSize: 30, fontWeight: FontWeight.bold),
),
)
8. transform
The transform property allows developers to rotate the container. It can rotate the container in any direction. If we want to change the container coordinate in the parent widget.
In the below example, we will rotate the container on the z-axis and skewY.
Container(
height: 400,
width: 400,
transform: Matrix4.rotationZ(0.2),
alignment: Alignment.topRight,
child: Text(
"What is Container ? ",
style: TextStyle(
color: Colors.white, fontSize: 30, fontWeight: FontWeight.bold),
),
)
Container(
height: 400,
width: 400,
transform:Matrix4.skewY(0.3),
alignment: Alignment.topRight,
child: Text(
"What is Container ? ",
style: TextStyle(
color: Colors.white, fontSize: 30, fontWeight: FontWeight.bold),
),
)
9. constraints
This property is used when we want to add additional constraints to the child. It contains various constructors, such as tight, loose, expands, etc. Let's see how to use these constructors in our app:
A. Expand
A loose constraint, Creates box constraints that expand to fill other box constraints. it will set the widget width, and height as per the given constraints and dimensions.
Container(
height: 400,
width: 400,
constraints: BoxConstraints.expand(height: 50.0),
alignment: Alignment.topRight,
child: Text(
"What is Container ? ",
style: TextStyle(
color: Colors.white, fontSize: 30, fontWeight: FontWeight.bold),
),
)
B. Tight
A tight constraint offers a single possibility, a tight constraint has its maximum width equal to its minimum width and has its maximum height equal to its minimum height.
Container(
height: 400,
width: 400,
constraints:BoxConstraints.tight(Size size)
: minWidth = size.width,
maxWidth = size.width,
minHeight = size.height,
maxHeight = size.height;
alignment: Alignment.topRight,
child: Text(
"What is Container ? ",
style: TextStyle(
color: Colors.white, fontSize: 30, fontWeight: FontWeight.bold),
),
)
C. Loose
A loose constraint, sets the maximum width and height, but lets the widget be as small as it wants. In other words, a loose constraint has a minimum width and height both equal to zero:
Container(
height: 400,
width: 400,
constraints: BoxConstraints.loose(Size size)
: minWidth = 0.0,
maxWidth = size.width,
minHeight = 0.0,
maxHeight = size.height;
alignment: Alignment.topRight,
child: Text(
"What is Container ? ",
style: TextStyle(
color: Colors.white, fontSize: 30, fontWeight: FontWeight.bold),
),
)
Nice 👍
ReplyDeleteThank You
Delete