You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Flutter的Container中实现内阴影(斜角效果)且不使用Neumorphic包

Flutter 内置的 BoxShadow 默认生成外阴影,因此你之前调整offset只能得到外凸起的效果,不需要引入第三方Neumorphic包,直接启用BoxShadow的inner属性即可实现内阴影,组合外阴影就能达到你要的效果。

修改后的代码示例:

Container(
  height: 50,
  width: MediaQuery.of(context).size.width - MediaQuery.of(context).size.width*1/7,
  decoration: BoxDecoration(
    borderRadius: BorderRadius.circular(10),
    border: Border.all(color: OpimaAppTheme.buttonColor, width: 3),
    color: Colors.black54,
    boxShadow: [
      // 外阴影:实现外侧凸起效果
      BoxShadow(
        offset: Offset(2, 2),
        blurRadius: 5.0,
        color: OpimaAppTheme.buttonShadowColor,
      ),
      BoxShadow(
        offset: Offset(-2, -2),
        blurRadius: 5.0,
        color: OpimaAppTheme.buttonShadowColor.withOpacity(0.3),
      ),
      // 内阴影:实现内侧凹陷效果
      BoxShadow(
        offset: Offset(2, 2),
        blurRadius: 4.0,
        color: Colors.black.withOpacity(0.4),
        inner: true, // 核心属性,标记为内阴影
      ),
      BoxShadow(
        offset: Offset(-1, -1),
        blurRadius: 4.0,
        color: Colors.white.withOpacity(0.2),
        inner: true,
      ),
    ],
  ),
)

参数调整说明:

  • 你可以根据设计稿调整阴影的offset偏移量、blurRadius模糊度、颜色透明度,匹配你需要的视觉效果
  • 如果你的Flutter版本低于3.10,无法使用inner属性,可以通过嵌套两层Container实现:外层绘制外阴影,内层用LinearGradient模拟边缘渐变实现内阴影效果,不需要引入额外依赖。

内容的提问来源于stack exchange,提问作者dartKnightRises

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 01:45:03