toHex method
You can customize the output by setting leadingHashSign to include or omit the '#' symbol.
You can also include the alpha (transparency) value in the hex string by setting includeAlpha.
Example:
Color color = Color(0xFF42A5F5);
String hex = color.toHex(); // Output: #42A5F5
String hexWithAlpha = color.toHex(includeAlpha: true); // Output: #FF42A5F5
Implementation
String toHex({bool leadingHashSign = true, bool includeAlpha = false}) =>
'${leadingHashSign ? '#' : ''}'
'${includeAlpha ? a.toInt().toRadixString(16).padLeft(2, '0') : ''}'
'${r.toInt().toRadixString(16).padLeft(2, '0')}'
'${g.toInt().toRadixString(16).padLeft(2, '0')}'
'${b.toInt().toRadixString(16).padLeft(2, '0')}';