maybeParse static method

LinearGradientValue? maybeParse(
  1. dynamic value
)

Implementation

static LinearGradientValue? maybeParse(dynamic value) {
  if (value == null || value is! String) return null;
  if (!value.startsWith('linear-gradient')) return null;

  final stripped =
      value.substring('linear-gradient('.length, value.length - 1).trim();

  // Convert rgba colors to hex so we can better parse the string
  final hexColors = _convertRgbColorsToHex(stripped);

  final firstComma = hexColors.indexOf(',');
  final angle =
      double.tryParse(hexColors.substring(0, hexColors.indexOf('deg')));

  final stops = hexColors.substring(firstComma + 1).trim().split(',');

  final colors = <ColorValue>[];
  final stopsList = <DimensionValue>[];
  for (var element in stops) {
    final splitted = element.trim().split(' ');

    final color = ColorValue.maybeParse(splitted[0].trim())!;
    colors.add(color);

    final stop = DimensionValue.maybeParse(splitted[1].trim())!;
    stopsList.add(stop);
  }

  return LinearGradientValue._(colors, stopsList, angle ?? 0);
}