Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 在渐变停止时获得颜色_Flutter - Fatal编程技术网

Flutter 在渐变停止时获得颜色

Flutter 在渐变停止时获得颜色,flutter,Flutter,在创建渐变后,是否有任何方法可以在特定的停止点找到颜色 Gradient g=扫掠梯度( 中心:分馏loffset.center, startAngle:0.0, endAngle:math.pi*2, 颜色:常量[ 颜色(0xFF4285F4),//蓝色 颜色(0xFF34A853),//绿色 颜色(0xFFFBBC05),//黄色 颜色(0xFFEA4335),//红色 颜色(0xFF4285F4),//再次为蓝色以无缝过渡到开始 ], 停止:常数[0.0,0.25,0.5,0.75,1.0

在创建渐变后,是否有任何方法可以在特定的停止点找到颜色

Gradient g=扫掠梯度(
中心:分馏loffset.center,
startAngle:0.0,
endAngle:math.pi*2,
颜色:常量[
颜色(0xFF4285F4),//蓝色
颜色(0xFF34A853),//绿色
颜色(0xFFFBBC05),//黄色
颜色(0xFFEA4335),//红色
颜色(0xFF4285F4),//再次为蓝色以无缝过渡到开始
],
停止:常数[0.0,0.25,0.5,0.75,1.0],
)

我可以做一些类似于
g.colorAt(0.95)
的事情来获得颜色吗?

您可以使用类似于在两种颜色
a
b
之间的线性插值来获得值
t

使用此选项,我们可以对渐变进行扩展,并创建方法
colorAt(double)

现在返回紫色的
颜色(0xFF6377CD)

显然,这并不精确,因为渐变不一定使用lerp,但您可能会发现它很有用。

您可以使用类似于在两种颜色
a
b
之间的值
t
进行线性插值

使用此选项,我们可以对渐变进行扩展,并创建方法
colorAt(double)

现在返回紫色的
颜色(0xFF6377CD)
。 显然,这并不精确,因为gradient不一定使用lerp,但您可能会发现它很有用

extension GradientColorAt on Gradient {
  Color colorAt(double stop) {
    assert(0 <= stop && stop <= 1);

    final nextStopIndex = stops.indexWhere((element) => element >= stop);
    if (nextStopIndex == -1) {
      // happens when the last stop is not 1, where we return the last given color
      return colors.last;
    }
    if (nextStopIndex == 0) {
      // happens when the first stop is not 0, where we return the first given color
      return colors.first;
    }
    return Color.lerp(
      colors[nextStopIndex - 1],
      colors[nextStopIndex],
      (stop - stops[nextStopIndex - 1]) / (stops[nextStopIndex] - stops[nextStopIndex - 1]),
    );
  }
}
g.colorAt(0.95);