colorToHex function

String colorToHex(
  1. Color color, {
  2. bool includeHashSign = false,
  3. bool enableAlpha = true,
  4. bool toUpperCase = true,
})

Converts a Flutter Color to a hex string.

Options:

  • includeHashSign — Prepend # to the output.
  • enableAlpha — Include the alpha channel in the output.
  • toUpperCase — Return the hex in uppercase (default: true).

Implementation

String colorToHex(
  Color color, {
  bool includeHashSign = false,
  bool enableAlpha = true,
  bool toUpperCase = true,
}) {
  final String hex =
      (includeHashSign ? '#' : '') +
      (enableAlpha ? _padRadix(color.alpha) : '') +
      _padRadix(color.red) +
      _padRadix(color.green) +
      _padRadix(color.blue);
  return toUpperCase ? hex.toUpperCase() : hex;
}