generateColorByString static method

Color? generateColorByString(
  1. String hexColor, {
  2. Color defaultColor = const Color(0xffffffff),
})

将标签 color 转为 颜色

Implementation

static Color? generateColorByString(
  String hexColor, {
  Color defaultColor = const Color(0xffffffff),
}) {
  Color? color =
      BaseThemeConfig.instance.getConfig().commonConfig.brandPrimary;
  if (hexColor.isEmpty) return color;
  hexColor = hexColor.toUpperCase().replaceAll('#', '');
  hexColor = hexColor.replaceAll('0X', '');
  if (hexColor.length == 6) {
    hexColor = 'FF' + hexColor;
  }

  try {
    color = Color(int.parse(hexColor, radix: 16));
  } catch (_) {}
  return color ?? defaultColor;
}