gradient static method
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 Map<String, dynamic>? gradient(Gradient? gradient) {
if (gradient == null) return null;
switch (gradient) {
case LinearGradient():
return NotNullMap.from({
'type': 'linear',
'begin': alignment(gradient.begin),
'end': alignment(gradient.end),
'colors': list<Color, dynamic>(gradient.colors, color),
'stops': list<double, double>(gradient.stops),
'tileMode': enumValue(gradient.tileMode)
});
case RadialGradient():
return NotNullMap.from({
'type': 'radial',
'center': alignment(gradient.center),
'colors': list<Color, dynamic>(gradient.colors, color),
'stops': list<double, double>(gradient.stops),
'tileMode': enumValue(gradient.tileMode),
'focal': alignment(gradient.focal),
'focalRadius': gradient.focalRadius
});
case SweepGradient():
return NotNullMap.from({
'type': 'sweep',
'center': alignment(gradient.center),
'startAngle': gradient.startAngle,
'endAngle': gradient.endAngle,
'colors': list<Color, dynamic>(gradient.colors, color),
'stops': list<double, double>(gradient.stops),
'tileMode': enumValue(gradient.tileMode),
});
default:
final encoder = gradientEncoders[gradient.runtimeType];
if (encoder == null) return null;
return encoder(gradient);
}
}