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

Flutter 如何实现覆盖四边的LinearGradient线性渐变效果?

效果示意图

实现方案

你原来的LinearGradient只设置了单一方向的渐变,因此只能覆盖两条对边,要实现四条边都有渐变过渡,可以用下面两种方案:

方案1:双层线性渐变叠加

通过嵌套两层Container,分别设置横向和纵向的线性渐变,叠加后即可覆盖四条边:

Container(
  decoration: BoxDecoration(
    // 横向渐变:处理左右两条边
    gradient: LinearGradient(
      colors: [
        kRecordButtonColor.withOpacity(0.4),
        kRecordButtonColor,
        kRecordButtonColor,
        kRecordButtonColor.withOpacity(0.4),
      ],
      stops: const [0, 0.2, 0.8, 1],
    ),
  ),
  child: Container(
    decoration: BoxDecoration(
      // 纵向渐变:处理上下两条边
      gradient: LinearGradient(
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
        colors: [
          kRecordButtonColor.withOpacity(0.4),
          Colors.transparent,
          Colors.transparent,
          kRecordButtonColor.withOpacity(0.4),
        ],
        stops: const [0, 0.2, 0.8, 1],
      ),
    ),
    // 这里放置你的子组件内容
  ),
)

方案2:径向渐变简化实现

如果不需要区分不同边的渐变参数,用径向渐变实现更简洁,可直接覆盖四边:

Container(
  decoration: BoxDecoration(
    gradient: RadialGradient(
      center: Alignment.center,
      radius: 0.8,
      colors: [
        kRecordButtonColor,
        kRecordButtonColor.withOpacity(0.4),
      ],
      stops: const [0.8, 1],
    ),
  ),
  // 这里放置你的子组件内容
)

内容的提问来源于stack exchange,提问作者who-aditya-nawandar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 06:57:02