gradient function

LinearGradient gradient({
  1. required List<Color> colors,
  2. List<double>? stops,
  3. Direction direction = Direction.toRight,
})

生成线性渐变色

创建一个可配置方向的线性渐变对象

@param colors 颜色数组 - 渐变色使用的颜色列表(必须) @param stops 渐变位置 - 颜色对应的位置数组(0.0-1.0),可选 @param direction 渐变方向 - 使用Direction枚举指定渐变方向,默认为向右 @return 返回配置好的LinearGradient渐变对象

Implementation

LinearGradient gradient(
    {required List<Color> colors,
    List<double>? stops,
    Direction direction = Direction.toRight}) {
  late AlignmentGeometry begin;
  late AlignmentGeometry end;
  switch (direction) {
    case Direction.toDown:
      begin = Alignment.topLeft;
      end = Alignment.bottomLeft;
      break;
    case Direction.toUp:
      begin = Alignment.bottomLeft;
      end = Alignment.topLeft;
      break;
    case Direction.toRight:
      begin = Alignment.topLeft;
      end = Alignment.topRight;
      break;
    case Direction.toLeft:
      begin = Alignment.topRight;
      end = Alignment.topLeft;
      break;
    case Direction.toTopLeft:
      begin = Alignment.bottomRight;
      end = Alignment.topLeft;
      break;
    case Direction.toTopRight:
      begin = Alignment.bottomLeft;
      end = Alignment.topRight;
      break;
    case Direction.toBottomLeft:
      begin = Alignment.topRight;
      end = Alignment.bottomLeft;
      break;
    case Direction.toBottomRight:
      begin = Alignment.topLeft;
      end = Alignment.bottomRight;
      break;
  }
  return LinearGradient(colors: colors, stops: stops, begin: begin, end: end);
}