Flutter Widget In Detail: Container
Detailed Explanation of Container Widget

Search for a command to run...
Detailed Explanation of Container Widget

No comments yet. Be the first to comment.
In this series, I am going to explain all the important Flutter widgets in detail.
How can You build a responsive Flutter app using Relative values rather than Pixel values?
Lessons from Code, Content, and Consistency

Reliability basics: Outbox pattern + why it matters

Introducing Kafka/Redpanda + move to event-driven workflow

Connect Orders ↔ Inventory (first working service-to-service flow)

Inventory service + gRPC + proto contracts

color, padding, margin, etc.
For example, Container(
margin: const EdgeInsets.all(10.0),
color: Colors.blue,
width: 48.0,
height: 48.0,
)

It's not required to have a child. We can create an empty container too.
child :Text, Row, Column , Stack etc.Container(
child: Text("Hello Container")
)

alignment :topLeft, topCenter, topRight, centerLeft, center, centerRight, bottomLeft, bottomCenter, bottomRightContainer(
alignment: Alignment.bottomRight,
color: Colors.blue,
width: 150.0,
height: 100.0,
child: Text("Hello Container")
)

color :Container(
alignment: Alignment.bottomRight,
color: CupertinoColors.activeGreen, // or Colors.green
child: Text("Hello Container")
)

constraintstightForFinite, loose, tight, tightFor, etc.Container(
constraints: BoxConstraints.tightForFinite(width: 200.0,height:100.0),
alignment: Alignment.center,
color: CupertinoColors.activeGreen,
child: Text("Hello Container")
)
width or height were given then the constraints will be infinite, which means the box will take all the available space.
loose constraint is nothing but an axis with a minimum constraint of 0.0. It takes Size(width,height) as an input.Container(
constraints: BoxConstraints.loose(Size(100.0,150.0)),
)

You can define maxWidth, maxHeight, minWidth, minHeight also in BoxConstraints constructor.
BoxConstraints(
maxHeight: 100,
maxWidth: 100,
minHeight: 80,
minWidth: 80
),
decoration :BoxDecoration constructor as an input. This constructor
provides a variety of ways to draw a box.border, shadow , gradients, image, alignment etc.Container(
decoration: BoxDecoration(
// Note: You can't have a property of "color" inside a container now as you've already defined in the decoration
color:Colors.purple,
borderRadius: BorderRadius.circular(10.0),
border:Border.all(color: Colors.red),
boxShadow: [
BoxShadow(
color: Colors.green,
blurRadius: 5.0,
spreadRadius: 5.0,
),
]
),

Now there are 4 more properties remaining of BoxDecoration i.e, gradient and image, backgroundBlendMode and shape. Let's see how to use them
Image :Container(
decoration: BoxDecoration(
image:DecorationImage(image: NetworkImage("https://url.com/flutter-logo.png")),
border:Border.all(color:Colors.black),
gradient: LinearGradient(
colors: [
Colors.pinkAccent,
Colors.blueAccent
])
),
)

image property you can pass other type of images like AssetImage FileImage, MemoryImage ,etc. shape :circular,rectangular, or you can also specify thevalues` for custom shapes.Container(
decoration:BoxDecoration(
color:Colors.blueAccent,
shape: BoxShape.circle // or BoxShape.rectangle
),
height:80.0,
width: 120.0,
child: Center(child:Text("Circle")),
),

backgroundBlendMode :color, colorBurn, darken, difference, dst, hardLight, etc.Container(
decoration:BoxDecoration(
backgroundBlendMode: BlendMode.difference,
// or BlendMode.xor, BlendMode.darken etc
color:Colors.blueAccent,
shape: BoxShape.circle
),
height:80.0,
width: 120.0,
child: Center(
child: Text(
"xor",
style:TextStyle(color:Colors.white)
)
),
),
blendMode. Take a look.
gradient:LinearGradient() :
colors: List of colorsbegin: Offset at which the gradient is placed.end: Offset at which the gradient stops placed.stops: A list of values from 0.0 to 1.0 that denote fractions along the gradient- Example: as explained above
RadialGradient() :
- Same as LinearGradient, But this also takes properties like
radius,focal,tileMode,colors: List of colorsstops: A list of values from 0.0 to 1.0 that denote fractions along the gradient.focal: The focal point of the gradient. If specified, the gradient will appear to be focused along the vector from center to focal.focalRadius: The radius of the focal point of the gradient.radius: The radius of the gradient- Example :
Container( decoration: BoxDecoration( border: Border.all(color:Colors.black), gradient: RadialGradient( colors: [Colors.green, Colors.blue, Colors.orange, Colors.pink], stops: [0.2, 0.5, 0.7, 1], focal: Alignment(-0.1, 0.2), focalRadius: 1, radius: 0.1 ), ),SweepGradient() :
- Similar to Linear and Radial Gradient, however, takes two different properties i.e,
startAngle,endAnglestartAngle: The angle in radians at which stop 0.0 of the gradient is placed.endAngle: The angle in radians at which stop 1.0 of the gradient is placed.- Example :
Container( decoration: BoxDecoration( border: Border.all(color:Colors.black), gradient: SweepGradient( colors: [Colors.blue, Colors.green, Colors.yellow, Colors.red, Colors.blue], stops: [0.0, 0.25, 0.5, 0.75, 1], ), ), )- After applying
startAngleandendAngle
margin:Container(
color:Colors.teal,
alignment: Alignment.center,
width: 150.0,
height: 100.0,
margin: EdgeInsets.only(left:10.0)
)

padding:Container(
color:Colors.teal,
alignment: Alignment.center,
width: 150.0,
height: 100.0,
padding: EdgeInsets.all(10.0)
)

margin and padding takes EdgeInsets class as an input. EdgeInsets has many constructor which is helpful to give margin/padding from different sides, some of them are:EdgeInsets.all(): To gives space from all the sides.EdgeInsets.only(): To give space from perticular side i.e left, right, top, bottom.EdgeInsets.symmetric(): To give space horizontally and verticallytransform:Matrix4 class, which has many useful constructor like, rotationX(), rotationY(), rotationZ(), skew(), translation(), etcContainer(
transform: Matrix4.rotationZ(0.1),
width:250.0,
height:150.0,
color:Colors.teal,
child:Center(
child:Text("Hello Container")
)
)

rotationX(), rotationY(), rotationZ(), skew(), translation() properties to see cool changestransformAlignment:transform is specified.topLeft, topCenter, topRight, centerLeft, center, centerRight, bottomLeft, bottomCenter, bottomRight.Container(
height:80.0,
width: 120.0,
color:Colors.green,
child: Center(child:Text("topCenter")),
transform: Matrix4.rotationZ(0.5),
transformAlignment: Alignment.topCenter,
)

foregroundDecoration:decoration property decorates/paints the box from behind the child. decoration example, but instead of the decoration I'm putting foregroundDecoration.Container(
foregroundDecoration: BoxDecoration(
color:Colors.purple,
borderRadius: BorderRadius.circular(10.0),
border:Border.all(color: Colors.red),
boxShadow: [
BoxShadow(
color: Colors.green,
blurRadius: 5.0,
spreadRadius: 5.0,
),
]
),
child : //....
),

Text is not visible. It is because the foregroundDecoration has painted the whole thing in front of our child widget.