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 可能会裁掉阴影,需要给父组件留空间
相关推荐
悟空瞎说6 小时前
Flutter 架构详解:新手必懂底层原理
flutter
SoaringHeart8 小时前
Flutter最佳实践:IM聊天文字链接自动识别跳转
前端·flutter
恋猫de小郭12 小时前
KMP / CMP 鸿蒙版本 Beta 发布,他有什么特别之处?
android·前端·flutter
风华圆舞1 天前
Flutter + 鸿蒙 Intents Kit:页面直达能力的完整接入方案
flutter·ui·华为·harmonyos
韩曙亮1 天前
【Flutter】Flutter 组件 ④ ( 组件渲染 的 三棵树理论 | Widget 树 → Element 树 → RenderObject 树 )
flutter·element·widget·renderobject
恋猫de小郭1 天前
Android 17 正式版发布,全新 AI 和各种破坏性更新
android·前端·flutter
kingbal1 天前
Windows:flutter环境搭建
windows·flutter
911hzh1 天前
Flutter MethodChannel 跨端通信框架 zh_native_channel:快速入门、优势分析与 Pigeon 对比
flutter
911hzh1 天前
Flutter 快速搭建新项目:用 Flutter Foundation Kit 一条命令生成带基础架构的 App 模板
flutter