toHex method

String toHex({
  1. bool leadingHashSign = true,
  2. bool includeAlpha = false,
})

Converts the Flutter color to a hex string.

Example:

Color color = Colors.red;
String hexString = color.toHex();
print('Hex Color: $hexString'); // Output: #FF0000

Implementation

String toHex({bool leadingHashSign = true, bool includeAlpha = false}) =>
    '${leadingHashSign ? '#' : ''}'
    '${includeAlpha ? alpha.toRadixString(16).padLeft(2, '0') : ''}'
    '${red.toRadixString(16).padLeft(2, '0')}'
    '${green.toRadixString(16).padLeft(2, '0')}'
    '${blue.toRadixString(16).padLeft(2, '0')}';