linearGradientShader function

Shader linearGradientShader(
  1. Size size,
  2. List<Color> colors,
  3. int angleTenths
)

A linear gradient across size through evenly spaced colors (two or more) at angleTenths (tenths of a degree; 0 = top → bottom, turning clockwise), corner to corner along that direction: the generic background gradient, for family scenes too.

Implementation

ui.Shader linearGradientShader(Size size, List<Color> colors, int angleTenths) {
  final stops = [
    for (var i = 0; i < colors.length; i++) i / (colors.length - 1),
  ];
  final radians = angleTenths / 10 * math.pi / 180;
  final dir = Offset(-math.sin(radians), math.cos(radians));
  final half = (dir.dx.abs() * size.width + dir.dy.abs() * size.height) / 2;
  final c = size.center(Offset.zero);
  return ui.Gradient.linear(c - dir * half, c + dir * half, colors, stops);
}