parseDesignColor function
Parses a CSS color string (hex or rgba) into a Flutter Color.
Implementation
Color? parseDesignColor(dynamic colorValue) {
if (colorValue == null || colorValue.toString().isEmpty) return null;
String colorStr = colorValue.toString().trim();
if (colorStr.startsWith('rgba(')) {
final match =
RegExp(r'rgba\((\d+),\s*(\d+),\s*(\d+),\s*([\d.]+)\)')
.firstMatch(colorStr);
if (match != null) {
return Color.fromRGBO(
int.parse(match.group(1)!),
int.parse(match.group(2)!),
int.parse(match.group(3)!),
double.parse(match.group(4)!),
);
}
}
if (colorStr.startsWith('#')) {
colorStr = colorStr.substring(1);
if (colorStr.length == 6) {
return Color(int.parse('FF$colorStr', radix: 16));
} else if (colorStr.length == 8) {
return Color(int.parse(colorStr, radix: 16));
}
}
return null;
}