kValidHexPattern top-level constant

String const kValidHexPattern

RegExp pattern for validation HEX color String inputs, allows only:

  • exactly 1 to 8 digits in HEX format,
  • only Latin A-F characters, case insensitive,
  • and integer numbers 0,1,2,3,4,5,6,7,8,9,
  • with optional hash (#) symbol at the beginning (not calculated in length).
final RegExp hexInputValidator = RegExp(kValidHexPattern);
if (hexInputValidator.hasMatch(hex)) print('$hex might be a valid HEX color');

Reference: https://en.wikipedia.org/wiki/Web_colors#Hex_triplet

Implementation

const String kValidHexPattern = r'^#?[0-9a-fA-F]{1,8}';