getGradient method

Gradient? getGradient(
  1. Map? spec
)

Implementation

Gradient? getGradient(Map? spec) {
  if (spec == null || spec.isEmpty || spec["colors"] == null) {
    return null;
  }

  final colors = <Color>[];
  final colorParts = (spec["colors"] as String).split(RegExp(r'[|\s]'));
  for (var part in colorParts) {
    var color = tryParseColor(part);
    if (color != null) {
      colors.add(color);
    }
  }

  List<double>? stops;
  final stopParts = (spec["stops"] as String).split(RegExp(r'[|\s]'));
  if (stopParts.length == colors.length) {
    stops = <double>[];
    for (var part in stopParts) {
      stops.add(parseDouble(part));
    }
  }

  final type = spec["_type"] ?? "default";
  switch (type) {
    case "SweepGradient":
      return SweepGradient(
        colors: colors,
        stops: stops,
        startAngle: parseDouble(spec["startAngle"], defaultValue: 0.0),
        endAngle: parseDouble(spec["endAngle"], defaultValue: math.pi * 2),
        center: getAlignment(spec["center"]) ?? Alignment.center,
      );
    case "RadialGradient":
      return RadialGradient(
        colors: colors,
        stops: stops,
        center: getAlignment(spec["center"]) ?? Alignment.center,
        focal: getAlignment(spec["focal"]),
        focalRadius: parseDouble(spec["focalRadius"]),
        radius: parseDouble(spec["radius"], defaultValue: 0.5),
      );
    default:
      return LinearGradient(
        colors: colors,
        stops: stops,
        begin: getAlignment(spec["begin"]) ?? Alignment.centerLeft,
        end: getAlignment(spec["end"]) ?? Alignment.centerRight,
      );
  }
}