【问题标题】:Flutter how to paint custom gradientsFlutter 如何绘制自定义渐变
【发布时间】:2020-07-03 20:16:02
【问题描述】:

我最近在尝试使用渐变创建跟随 AppBar 时遇到了一个问题。

当我尝试用色彩来复制这个设计时 玫瑰=颜色(0xFFec15ee), 紫色 = 颜色(0xFF8561f5)和 blue = Color(0xFF1eaefc) 并相应地设置对齐属性,它以某种方式给了我没有预期的结果

BoxDecoration(
      gradient: LinearGradient(
        colors: [
          AppColors.roseGradientColor,
          AppColors.purpleInAppGradientColor,
          AppColors.blueInAppGradientColor
        ],
        stops: [
          0.0,
          0.05,
          1.0
        ],
        begin: Alignment.bottomLeft,
        end: Alignment.topRight
      ),
        borderRadius: BorderRadius.only(bottomLeft: Radius.circular(25),bottomRight: Radius.circular(25))
    )

想象一下不透明度不会存在。如您所见,我只希望玫瑰在 bottomLeft 对齐,而不是向上扩展至 topLeft,如示例所示。

我的问题是我该怎么做。在 CustomPainter 中必须有一种方法可以做到这一点,但我还没有找到正确的方法。

【问题讨论】:

  • 代替Alignment.topRight 使用远远高于topAlignment - 请参阅Alignment(double, double) 构造函数

标签: user-interface flutter dart gradient custom-painting


【解决方案1】:

pskinks 的评论是正确的,我完全忽略了这个选项。 使用 Alignment(x,y) 是这里的关键。

这是我上述问题的解决方案。

BoxDecoration(
      gradient: LinearGradient(
        colors: [
          AppColors.roseGradientColor,
          AppColors.purpleInAppGradientColor,
          AppColors.blueInAppGradientColor
        ],
        begin: Alignment(-0.7,12),
        end: Alignment(1,-2),
      ),
        borderRadius: BorderRadius.only(bottomLeft: Radius.circular(25),bottomRight: Radius.circular(25))
    )

【讨论】:

  • 好的,但是为什么 -0.7begin 对齐?为什么不-1-1 代表“左”
  • 老实说,我用 -1、-5、-10、-0.2、-0.5 等测试了设计,我发现 -0.7 是我想要的最接近的设计。这个值是通过反复试验找到的 100% ;)
  • 最重要的是最终结果看起来还可以;-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-24
  • 1970-01-01
  • 1970-01-01
  • 2014-10-22
  • 2021-12-22
相关资源
最近更新 更多