# Explore Container Widget in Flutter

<span class="r"></span>

# Explore Container Widget in Flutter


A tutorial to help you understand the Container Widget in Flutter.

![Image for post](https://miro.medium.com/max/60/1*5oIZCO9KRxpjGr1tpFcI4Q.png?q=20)

<noscript><img alt="Image for post" class="s t u by ai" src="https://miro.medium.com/max/800/1*5oIZCO9KRxpjGr1tpFcI4Q.png" width="400" height="200" srcSet="https://miro.medium.com/max/552/1*5oIZCO9KRxpjGr1tpFcI4Q.png 276w, https://miro.medium.com/max/800/1*5oIZCO9KRxpjGr1tpFcI4Q.png 400w" sizes="400px"/></noscript>

## Try out Container widget and it’s properties directly from [DartPad](https://dartpad.dev/3ac31b204e94d54403faa9a719e5b5e6)

# Introduction to Container Widget

It is a Widget that combines painting, positioning and sizing of the widget.

Syntax to create a Container-


```
Container()
```


This will create a container and it try to size itself as small as possible.

# How to define a child of Conatiner


```
Container(child: FlutterLogo(),
         color:Colors.red
)
```


The output is-

![Image for post](https://miro.medium.com/max/60/1*wZxXsOWTzKEBXpsKnljRoA.png?q=20)

<noscript><img alt="Image for post" class="s t u by ai" src="https://miro.medium.com/max/614/1*wZxXsOWTzKEBXpsKnljRoA.png" width="307" height="248" srcSet="https://miro.medium.com/max/552/1*wZxXsOWTzKEBXpsKnljRoA.png 276w, https://miro.medium.com/max/614/1*wZxXsOWTzKEBXpsKnljRoA.png 307w" sizes="307px"/></noscript>

Now you can see that we declare only a child inside our Container but we do not assign any height and width to it .So Container by default try to Size itself according to Child size.That is why our Container is soo small.

# How to define Height and Width for Container


```
Container(child: FlutterLogo(), 
          height: 100, width: 100
)
```


The output is-

![Image for post](https://miro.medium.com/max/56/1*uGjjQ6uEwCCGBGNcIiqbcw.png?q=20)

<noscript><img alt="Image for post" class="s t u by ai" src="https://miro.medium.com/max/614/1*uGjjQ6uEwCCGBGNcIiqbcw.png" width="307" height="329" srcSet="https://miro.medium.com/max/552/1*uGjjQ6uEwCCGBGNcIiqbcw.png 276w, https://miro.medium.com/max/614/1*uGjjQ6uEwCCGBGNcIiqbcw.png 307w" sizes="307px"/></noscript>

Now our Flutter Logo size increased.

# How to define a Color to the Container

Simply use the color property


```
Container(child: FlutterLogo(), 
          height: 100, width: 100, 
          color: Colors.red
)
```


The output is-

![Image for post](https://miro.medium.com/max/54/1*3LpTPTjDFTUD8qBQupJ-Dw.png?q=20)

<noscript><img alt="Image for post" class="s t u by ai" src="https://miro.medium.com/max/614/1*3LpTPTjDFTUD8qBQupJ-Dw.png" width="307" height="340" srcSet="https://miro.medium.com/max/552/1*3LpTPTjDFTUD8qBQupJ-Dw.png 276w, https://miro.medium.com/max/614/1*3LpTPTjDFTUD8qBQupJ-Dw.png 307w" sizes="307px"/></noscript>

# How to define Margin to our Container

Margin is basically the empty space that surround the decoration and child of container.


```
Container(
    child: FlutterLogo(),
    height: 400,
    width: 200,
    color: Colors.red,
    margin: EdgeInsets.all(60)
)
```


The output is-

![Image for post](https://miro.medium.com/max/38/1*763tNSKTbfanVES1URXoBg.png?q=20)

<noscript><img alt="Image for post" class="s t u by ai" src="https://miro.medium.com/max/614/1*763tNSKTbfanVES1URXoBg.png" width="307" height="484" srcSet="https://miro.medium.com/max/552/1*763tNSKTbfanVES1URXoBg.png 276w, https://miro.medium.com/max/614/1*763tNSKTbfanVES1URXoBg.png 307w" sizes="307px"/></noscript>

The green space in the margin that surround the Container.

For more details on EdgeInsets refer my SizedBox Widget article.

# How to define Padding to Container

To understand it easily let’s take the above example


```
Container(
    child: FlutterLogo(),
    height: 400,
    width: 200,
    color: Colors.red,
    margin: EdgeInsets.all(60),
    padding: EdgeInsets.all(50)
)
```


The output is-

![Image for post](https://miro.medium.com/max/38/1*r9A7rDlvT0i46nQMlDRmTQ.png?q=20)

<noscript><img alt="Image for post" class="s t u by ai" src="https://miro.medium.com/max/614/1*r9A7rDlvT0i46nQMlDRmTQ.png" width="307" height="484" srcSet="https://miro.medium.com/max/552/1*r9A7rDlvT0i46nQMlDRmTQ.png 276w, https://miro.medium.com/max/614/1*r9A7rDlvT0i46nQMlDRmTQ.png 307w" sizes="307px"/></noscript>

Now in the image the Logo shrinks because we define padding to Container.

# How to align Our child Inside Container

If we want to align our child we use the alignment property which can have values-

bottonCenter,bottomLeft,bottomRight,center,centerLeft,centerRight,topLeft,topRight,topCenter

Let’s see an example how to define them-


```
Container(
    child: FlutterLogo(size: 50),
    height: 300,
    width: 300,
    color: Colors.red,
    alignment: Alignment.topCenter,
    margin: EdgeInsets.all(40)
)
```


The output is-

![Image for post](https://miro.medium.com/max/40/1*uBayzPqQfsQZWR6R-EpEpA.png?q=20)

<noscript><img alt="Image for post" class="s t u by ai" src="https://miro.medium.com/max/614/1*uBayzPqQfsQZWR6R-EpEpA.png" width="307" height="468" srcSet="https://miro.medium.com/max/552/1*uBayzPqQfsQZWR6R-EpEpA.png 276w, https://miro.medium.com/max/614/1*uBayzPqQfsQZWR6R-EpEpA.png 307w" sizes="307px"/></noscript>

Now use any value instead of topCenter and try to find what happens with the Widget.

# How to decorate our Container

We can use the decoration property which can have any value either

1.  BoxDecoration
2.  FlutterLogoDecoration
3.  ShapeDecoration

# Let First dive deep in BoxDecoration

It is used to paint a box.Box may have a border, body, shadow


```
Container(
    child: Text("Hello"),
    height: 200,
    width: 200,
    decoration: BoxDecoration(color: Colors.redAccent)
)
```


The Output is-

![Image for post](https://miro.medium.com/max/44/1*tO06d8Bq974Qco9FEFv-Pw.png?q=20)

<noscript><img alt="Image for post" class="s t u by ai" src="https://miro.medium.com/max/614/1*tO06d8Bq974Qco9FEFv-Pw.png" width="307" height="415" srcSet="https://miro.medium.com/max/552/1*tO06d8Bq974Qco9FEFv-Pw.png 276w, https://miro.medium.com/max/614/1*tO06d8Bq974Qco9FEFv-Pw.png 307w" sizes="307px"/></noscript>

You can see that here we define color for our container using BoxDecoration class.

# How to apply Border to our Container

We can apply border same for all side or different for each side.Let’s see each one.

## For all side same border


```
Container(
    child: Center(child:Text("hello")),
    height: 200,
    width: 200,
    decoration: BoxDecoration(
      color: Colors.red,
      border: Border.all(width: 33, color: Colors.green),
    ))
```


The output is-

![Image for post](https://miro.medium.com/max/42/1*0MNCE_GFt4Fq7HXsnRjR2w.png?q=20)

<noscript><img alt="Image for post" class="s t u by ai" src="https://miro.medium.com/max/614/1*0MNCE_GFt4Fq7HXsnRjR2w.png" width="307" height="432" srcSet="https://miro.medium.com/max/552/1*0MNCE_GFt4Fq7HXsnRjR2w.png 276w, https://miro.medium.com/max/614/1*0MNCE_GFt4Fq7HXsnRjR2w.png 307w" sizes="307px"/></noscript>

As you can see we have a border of same width for all side.

## How to define defferent border for each side


```
Container(
    child: Center(child:Text("Hello")),
    height: 200,
    width: 200,
    decoration: BoxDecoration(
        color: Colors.redAccent,
        border: Border(
            top: BorderSide(width: 5, color: Colors.green),
            left: BorderSide(width: 10, color: Colors.yellow),
            right: BorderSide(width: 15, color: Colors.blue),
            bottom: BorderSide(width: 25, color: Colors.grey),

        )))
```


The Output is-

![Image for post](https://miro.medium.com/max/44/1*sm6jb7HchPAXLeaX0lcreg.png?q=20)

<noscript><img alt="Image for post" class="s t u by ai" src="https://miro.medium.com/max/614/1*sm6jb7HchPAXLeaX0lcreg.png" width="307" height="428" srcSet="https://miro.medium.com/max/552/1*sm6jb7HchPAXLeaX0lcreg.png 276w, https://miro.medium.com/max/614/1*sm6jb7HchPAXLeaX0lcreg.png 307w" sizes="307px"/></noscript>

Now here all four side of container have different color and width of border.

NOTE : You can also define border for any combination of side.

# How to define Border Radius

Border radius is used to curve the edges (style) of border.

## Rounded rectangle containers with border


```
Container(
    child:Center(child: Text("hello")),
    height: 200,
    width: 200,
    decoration: BoxDecoration(
        color: Colors.red,
        border: Border.all(width: 5,color: Colors.yellow),
        borderRadius: BorderRadius.circular(30)
))
```


The Output is-

![Image for post](https://miro.medium.com/max/42/1*UWcD0LZI3iPQjmQnaymBKg.png?q=20)

<noscript><img alt="Image for post" class="s t u by ai" src="https://miro.medium.com/max/614/1*UWcD0LZI3iPQjmQnaymBKg.png" width="307" height="439" srcSet="https://miro.medium.com/max/552/1*UWcD0LZI3iPQjmQnaymBKg.png 276w, https://miro.medium.com/max/614/1*UWcD0LZI3iPQjmQnaymBKg.png 307w" sizes="307px"/></noscript>

You can see the rounded yellow border in the image.

What if we Increases the value of radius.

## Circular Container


```
Container(
    child:Center(child: Text("hello")),
    height: 200,
    width: 200,
    decoration: BoxDecoration(
        color: Colors.red,
        border: Border.all(width: 5,color: Colors.yellow),
        borderRadius: BorderRadius.circular(200)
))
```


The Output is-

![Image for post](https://miro.medium.com/max/48/1*0fHnh0T--6jgUN_6Xmry4w.png?q=20)

<noscript><img alt="Image for post" class="s t u by ai" src="https://miro.medium.com/max/614/1*0fHnh0T--6jgUN_6Xmry4w.png" width="307" height="380" srcSet="https://miro.medium.com/max/552/1*0fHnh0T--6jgUN_6Xmry4w.png 276w, https://miro.medium.com/max/614/1*0fHnh0T--6jgUN_6Xmry4w.png 307w" sizes="307px"/></noscript>

Now you can see that we have a circular container .That looks nice.

## Conatiner with Horizontal radius(Left and Right)


```
Container(
      height: 200.0,
      width: 200.0,
      alignment: Alignment.center,
      decoration: BoxDecoration(
            borderRadius: BorderRadius.horizontal(
              left: Radius.circular(20.0),
              right: Radius.circular(80.0)
            ),
              color: Colors.green,
          ),
          child: Text('Hello'),
        )
```


The Output is-

![Image for post](https://miro.medium.com/max/38/1*NwO2pYEsYDDwCj_xEZXhZw.png?q=20)

<noscript><img alt="Image for post" class="s t u by ai" src="https://miro.medium.com/max/614/1*NwO2pYEsYDDwCj_xEZXhZw.png" width="307" height="478" srcSet="https://miro.medium.com/max/552/1*NwO2pYEsYDDwCj_xEZXhZw.png 276w, https://miro.medium.com/max/614/1*NwO2pYEsYDDwCj_xEZXhZw.png 307w" sizes="307px"/></noscript>

Now observe that the Left side edge are curved same and right side edge curve is same.

## Conatiner with Vertical radius(Top and Bottom)


```
Container(
      height: 200.0,
      width: 200.0,
      alignment: Alignment.center,
      decoration: BoxDecoration(
            borderRadius: BorderRadius.vertical(
              top: Radius.circular(20.0),
              bottom: Radius.circular(80.0)
            ),
              color: Colors.green,
          ),
          child: Text('Hello'),
        )
```


The output is-

![Image for post](https://miro.medium.com/max/44/1*_BuVeKEDyts2QM1EFpufNQ.png?q=20)

<noscript><img alt="Image for post" class="s t u by ai" src="https://miro.medium.com/max/614/1*_BuVeKEDyts2QM1EFpufNQ.png" width="307" height="416" srcSet="https://miro.medium.com/max/552/1*_BuVeKEDyts2QM1EFpufNQ.png 276w, https://miro.medium.com/max/614/1*_BuVeKEDyts2QM1EFpufNQ.png 307w" sizes="307px"/></noscript>

Now the border is same from top and same for bottom.

# Container with Image and Rounded Border


```
Container(
 height: 200.0,
 width: 200.0,
 decoration: BoxDecoration(
 borderRadius: BorderRadius.circular(20.0),
 color: Colors.white,
 image: DecorationImage(fit: BoxFit.cover,
 image: NetworkImage(‘[https://picsum.photos/200/300’](https://picsum.photos/200/300')))
 ),
 )
```


The output is-

![Image for post](https://miro.medium.com/max/52/1*jHKE0ANwIbSHRgHevanDcg.png?q=20)

<noscript><img alt="Image for post" class="s t u by ai" src="https://miro.medium.com/max/614/1*jHKE0ANwIbSHRgHevanDcg.png" width="307" height="354" srcSet="https://miro.medium.com/max/552/1*jHKE0ANwIbSHRgHevanDcg.png 276w, https://miro.medium.com/max/614/1*jHKE0ANwIbSHRgHevanDcg.png 307w" sizes="307px"/></noscript>

## Container with List of Box Shadow


```
Container(
          height: 100.0,
          width: 200.0,
          decoration: BoxDecoration(
              color: Colors.white,
              boxShadow: [
              BoxShadow(color: Colors.red, blurRadius: 0.0, spreadRadius:20 ),
              BoxShadow(color: Colors.green, blurRadius: 40.0, spreadRadius:30)
              ]
          ),
        )
```


The Output is-

![Image for post](https://miro.medium.com/max/38/1*5e8u9QwEySe58TPvmPoXjw.png?q=20)

<noscript><img alt="Image for post" class="s t u by ai" src="https://miro.medium.com/max/614/1*5e8u9QwEySe58TPvmPoXjw.png" width="307" height="488" srcSet="https://miro.medium.com/max/552/1*5e8u9QwEySe58TPvmPoXjw.png 276w, https://miro.medium.com/max/614/1*5e8u9QwEySe58TPvmPoXjw.png 307w" sizes="307px"/></noscript>


