Flutter BoxDecoration boxShadow 完整用法

boxShadowBoxDecoration 的属性,用来加阴影,接收一个数组,可以同时加多层阴影。


1. 最常用写法(单阴影)

dart

复制代码
BoxDecoration(
  boxShadow: [
    BoxShadow(
      color: Colors.black12,    // 阴影颜色
      blurRadius: 10,          // 模糊程度
      spreadRadius: 2,         // 扩散范围
      offset: Offset(0, 4),    // 偏移 (x, y)
    ),
  ],
)

2. 每个参数说明

  • color :阴影颜色,一般带透明度 Colors.black12 / 0x11000000
  • blurRadius:模糊半径,越大越模糊
  • spreadRadius:扩散大小,正数变大,负数变小
  • offset :偏移
    • Offset(0,0) 居中
    • Offset(0,4) 向下阴影
    • Offset(2,2) 右下阴影

3. 多层阴影(质感更强)

dart

复制代码
boxShadow: [
  BoxShadow(
    color: Color(0x08000000),
    blurRadius: 15,
    offset: Offset(0, 6),
  ),
  BoxShadow(
    color: Color(0x0A000000),
    blurRadius: 30,
    offset: Offset(0, 12),
  ),
]

4. 毛玻璃 + 圆角 + 阴影(你现在的组合)

dart

复制代码
ClipRRect(
  borderRadius: BorderRadius.circular(20),
  child: BackdropFilter(
    filter: ImageFilter.blur(sigmaX: 10, sigmaY: 10),
    child: Container(
      width: 300,
      height: 200,
      decoration: BoxDecoration(
        color: Colors.white.withOpacity(0.15),
        borderRadius: BorderRadius.circular(20),
        border: Border.all(
          color: Colors.white.withOpacity(0.2),
          width: 1,
        ),
        // 阴影
        boxShadow: [
          BoxShadow(
            color: Colors.black12,
            blurRadius: 20,
            spreadRadius: 1,
            offset: Offset(0, 8),
          ),
        ],
      ),
    ),
  ),
)

5. 小坑

  • 阴影要半透明颜色才自然
  • 阴影太大、太多层会卡顿
  • 加了阴影后,ClipRRect 可能会裁掉阴影,需要给父组件留空间
相关推荐
Eric_HYD41 分钟前
Flutter 字体字生效原理解析
flutter
maaath1 小时前
【无标题】Flutter for OpenHarmony 的文具手账应用开发实践
flutter·华为·harmonyos
里欧跑得慢1 小时前
Flutter 主题管理:构建一致的用户界面
前端·css·flutter·web
liulian091616 小时前
Flutter for OpenHarmony 跨平台开发:单位转换功能实战指南
flutter
千码君201616 小时前
Trae:一些关于flutter和 go前后端开发构建的分享
android·flutter·gradle·android-studio·trae·vibe code
maaath18 小时前
【maaath】Flutter for OpenHarmony 手表配饰应用实战开发
flutter·华为·harmonyos
maaath19 小时前
【maaath】Flutter for OpenHarmony 跨平台计算器应用开发实践
flutter·华为·harmonyos
maaath1 天前
【maaath】Flutter for OpenHarmony 闹钟时钟应用开发实战
flutter·华为·harmonyos
maaath1 天前
【maaath】Flutter for OpenHarmony 短信管理应用实战
flutter·华为·harmonyos
maaath1 天前
【maaath】Flutter for OpenHarmony打造跨平台便签备忘录应用
flutter·华为·harmonyos