BoxDecoration 是 Flutter 中用于控制 Container 等组件外观的装饰类,它提供了丰富的属性来设置背景、边框、圆角、阴影等样式。
BoxDecoration 的主要属性
color: Colors.blue,
image: DecorationImage(
image: AssetImage('assets/images/bg.png'),
fit: BoxFit.cover,
),
border: Border.all(
color: Colors.red,
width: 2.0,
),
borderRadius: BorderRadius.circular(10),
shape: BoxShape.circle,
gradient: LinearGradient(
colors: [Colors.blue, Colors.purple],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.2),
offset: Offset(2, 4),
blurRadius: 6,
),
],
backgroundBlendMode: BlendMode.multiply,
clipBehavior: Clip.hardEdge,
完整属性示例:
Container(
width: 100,
height: 100,
decoration: BoxDecoration(
color: Colors.blue, // 背景颜色
border: Border.all( // 边框
color: Colors.red,
width: 2.0,
),
borderRadius: BorderRadius.circular(15), // 圆角
boxShadow: [ // 阴影
BoxShadow(
color: Colors.black.withOpacity(0.2),
offset: Offset(3, 3),
blurRadius: 5,
),
],
gradient: LinearGradient( // 渐变
colors: [Colors.blue, Colors.green],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
image: DecorationImage( // 背景图片
image: AssetImage('assets/images/example.png'),
fit: BoxFit.cover,
),
),
)