If you’re learning Flutter, sooner or later you’ll come across something called BoxDecoration
. This class is used all the time, especially when you want to make your app’s UI look more stylish. Flutter is already famous for its design flexibility, and with BoxDecoration
, you can give your widgets background colors, borders, shadows, gradients, and so much more.
In this article, we’ll talk about the attributes of BoxDecoration()
, how to use them, and provide examples so you can fully understand. Don’t worry, the explanation will be casual and easy to digest, not as stiff as official documentation. Perfect for both beginners and those who are experimenting with Flutter UI.
What is BoxDecoration?
Before diving into its attributes, let’s first understand what BoxDecoration
actually is.
BoxDecoration
is a Flutter class used to decorate widgets, often combined with Container
. For example, when you create a plain Container
, it looks boring—a simple box without any style. But with BoxDecoration
, you can add background colors, gradients, images, borders, rounded corners, and even shadows.
Here’s a simple example:
Container(
width: 200,
height: 200,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(20),
boxShadow: [
BoxShadow(
color: Colors.black26,
blurRadius: 10,
offset: Offset(5, 5),
),
],
),
)
The result? A blue box with rounded corners and a soft shadow. Looks much nicer, right?
And all of this is possible because of the attributes in BoxDecoration
.
Attributes of BoxDecoration
Now let’s break down each attribute in BoxDecoration
.
1. color
This attribute sets the background color.
Example:
decoration: BoxDecoration(
color: Colors.red,
),
The container will have a plain red background.
If you only need a solid color without any extra style, this is enough.
2. image
Want to add a background image? Use the image
attribute with DecorationImage
.
Example:
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/bg.png'),
fit: BoxFit.cover,
),
),
-
image
→ the image you want to use (AssetImage, NetworkImage, etc). -
fit
→ how the image should scale (cover, contain, etc).
This is very useful for banners or profile sections.
3. border
This attribute creates borders around the container.
Example:
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
width: 3,
),
),
The result is a box with a thick black border.
You can also make borders on specific sides like top or bottom only.
4. borderRadius
If you don’t like sharp corners, borderRadius
makes them rounded.
Example:
decoration: BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.circular(15),
),
This creates a green box with smooth rounded edges.
Note: This doesn’t work if shape
is set to BoxShape.circle
.
5. boxShadow
Shadows give your UI a modern feel. Use boxShadow
for that.
Example:
decoration: BoxDecoration(
color: Colors.white,
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
spreadRadius: 2,
blurRadius: 10,
offset: Offset(3, 3),
),
],
),
-
color
→ shadow color. -
spreadRadius
→ how far the shadow spreads. -
blurRadius
→ how soft or blurry the shadow looks. -
offset
→ position of the shadow (x, y).
This is often used for card-style UIs.
6. gradient
If a solid color feels boring, use gradient
for a color transition effect.
Example with LinearGradient
:
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.blue, Colors.purple],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
),
You can also use RadialGradient
or SweepGradient
. Gradients make the UI look more dynamic and modern.
7. shape
The shape
attribute sets the shape of the box. Options:
-
BoxShape.rectangle
(default) → square or rectangle. -
BoxShape.circle
→ circle.
Example:
decoration: BoxDecoration(
color: Colors.orange,
shape: BoxShape.circle,
),
This gives you an orange circle. Perfect for avatars or icons.
Full Example with All Attributes
Here’s a more complete example combining multiple attributes:
Container(
width: 250,
height: 250,
decoration: BoxDecoration(
color: Colors.white,
image: DecorationImage(
image: AssetImage('assets/bg.jpg'),
fit: BoxFit.cover,
),
border: Border.all(
color: Colors.blueAccent,
width: 4,
),
borderRadius: BorderRadius.circular(20),
boxShadow: [
BoxShadow(
color: Colors.black26,
blurRadius: 8,
offset: Offset(4, 4),
),
],
gradient: LinearGradient(
colors: [Colors.pink, Colors.orange],
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
),
shape: BoxShape.rectangle,
),
)
Imagine the result: a stylish box with a gradient + image background, blue borders, rounded corners, and a soft shadow. That’s the magic of BoxDecoration
.
Tips for Using BoxDecoration
-
Use only what you need – Don’t apply every attribute at once; it might make the UI look messy.
-
Consider performance – Heavy effects like shadows, gradients, or big images can affect performance on low-end devices.
-
Use themes – If you use the same colors often, put them in
ThemeData
for consistency. -
Experiment! – Flutter UI is very flexible. Don’t be afraid to mix and match attributes.
BoxDecoration
is one of the key tools in Flutter for creating beautiful UIs. With attributes like color, image, border, borderRadius, boxShadow, gradient, and shape, you can transform a plain container into something visually appealing.
For beginners, start simple maybe just color and border. Once you’re comfortable, try gradients, rounded corners, or shadows. The more you experiment, the more creative your designs will become.
So, if someone asks you “what are the attributes of BoxDecoration?”
The answer is: color, image, border, borderRadius, boxShadow, gradient, shape.
0 Comments:
Post a Comment