colorToHex function

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

Formats color as a hex string.

By default the result is eight upper-case digits in AARRGGBB order with no #. Set includeHashSign to add a leading #, enableAlpha to false to leave out the alpha digits, and toUpperCase to false for lower-case letters.

colorToHex(Colors.orange);                     // 'FFFF9800'
colorToHex(Colors.orange, includeHashSign: true,
    enableAlpha: false);                       // '#FF9800'

Implementation

String colorToHex(
  Color color, {
  bool includeHashSign = false,
  bool enableAlpha = true,
  bool toUpperCase = true,
}) {
  final String hex =
      (includeHashSign ? '#' : '') +
      (enableAlpha ? _padRadix(color.alphaInt) : '') +
      _padRadix(color.redInt) +
      _padRadix(color.greenInt) +
      _padRadix(color.blueInt);
  return toUpperCase ? hex.toUpperCase() : hex;
}