gradient static method

Gradient? gradient(
  1. DataSource source,
  2. List<Object> key
)

Returns a Gradient from the specified map.

The type key specifies the kind of gradient.

A type of linear creates a LinearGradient using the keys begin (alignment, defaults to Alignment.centerLeft), end (alignment, defaults to Alignment.centerRight), colors (list of colorOrBlack, defaults to a two-element list with black and white), stops (list of doubleOrZero), and tileMode (enumValue of TileMode, defaults to TileMode.clamp), these keys each corresponding to the properties of BoxDecoration with the same name.

A type of radial creates a RadialGradient using the keys center (alignment, defaults to Alignment.center), radius' (double, defaults to 0.5), colors([list] of [colorOrBlack], defaults to a two-element list with black and white),stops([list] of [doubleOrZero]),tileMode([enumValue] of [TileMode], defaults to [TileMode.clamp]),focal(([alignment]), andfocalRadius` (double, defaults to zero), these keys each corresponding to the properties of BoxDecoration with the same name.

A type of linear creates a LinearGradient using the keys center (alignment, defaults to Alignment.center), startAngle (double, defaults to 0.0), endAngle (double, defaults to 2π), colors (list of colorOrBlack, defaults to a two-element list with black and white), stops (list of doubleOrZero), and tileMode (enumValue of TileMode, defaults to TileMode.clamp), these keys each corresponding to the properties of BoxDecoration with the same name.

The transform property of these gradient classes is not supported.

If the type is none of these, but is not null, then the type is looked up in gradientDecoders, and if an entry is found, this method defers to that callback.

Otherwise, returns null.

Implementation

// TODO(ianh): https://github.com/flutter/flutter/issues/87208
///
/// If the type is none of these, but is not null, then the type is looked up
/// in [gradientDecoders], and if an entry is found, this method defers to
/// that callback.
///
/// Otherwise, returns null.
static Gradient? gradient(DataSource source, List<Object> key) {
  final String? type = source.v<String>([...key, 'type']);
  switch (type) {
    case null:
      return null;
    case 'linear':
      return LinearGradient(
        begin: alignment(source, [...key, 'begin']) ?? Alignment.centerLeft,
        end: alignment(source, [...key, 'end']) ?? Alignment.centerRight,
        colors: list<Color>(source, [...key, 'colors'], colorOrBlack) ?? const <Color>[Color(0xFF000000), Color(0xFFFFFFFF)],
        stops: list<double>(source, [...key, 'stops'], doubleOrZero),
        tileMode: enumValue<TileMode>(TileMode.values, source, [...key, 'tileMode']) ?? TileMode.clamp,
        // transform: GradientTransformMatrix(matrix(source, [...key, 'transform'])), // blocked by https://github.com/flutter/flutter/issues/87208
      );
    case 'radial':
      return RadialGradient(
        center: alignment(source, [...key, 'center']) ?? Alignment.center,
        radius: source.v<double>([...key, 'radius']) ?? 0.5,
        colors: list<Color>(source, [...key, 'colors'], colorOrBlack) ?? const <Color>[Color(0xFF000000), Color(0xFFFFFFFF)],
        stops: list<double>(source, [...key, 'stops'], doubleOrZero),
        tileMode: enumValue<TileMode>(TileMode.values, source, [...key, 'tileMode']) ?? TileMode.clamp,
        focal: alignment(source, [...key, 'focal']),
        focalRadius: source.v<double>([...key, 'focalRadius']) ?? 0.0,
        // transform: GradientTransformMatrix(matrix(source, [...key, 'transform'])), // blocked by https://github.com/flutter/flutter/issues/87208
      );
    case 'sweep':
      return SweepGradient(
        center: alignment(source, [...key, 'center']) ?? Alignment.center,
        startAngle: source.v<double>([...key, 'startAngle']) ?? 0.0,
        endAngle: source.v<double>([...key, 'endAngle']) ?? math.pi * 2,
        colors: list<Color>(source, [...key, 'colors'], colorOrBlack) ?? const <Color>[Color(0xFF000000), Color(0xFFFFFFFF)],
        stops: list<double>(source, [...key, 'stops'], doubleOrZero),
        tileMode: enumValue<TileMode>(TileMode.values, source, [...key, 'tileMode']) ?? TileMode.clamp,
        // transform: GradientTransformMatrix(matrix(source, [...key, 'transform'])), // blocked by https://github.com/flutter/flutter/issues/87208
      );
    default:
      final ArgumentDecoder<Gradient?>? decoder = gradientDecoders[type];
      if (decoder == null) {
        return null;
      }
      return decoder(source, key);
  }
}