Line data Source code
1 : import 'package:flutter/material.dart'; 2 : 3 : extension HexColor on Color { 4 3 : static Color fromHex(String hexString) { 5 3 : final buffer = StringBuffer(); 6 14 : if (hexString.length == 6 || hexString.length == 7) buffer.write('ff'); 7 6 : buffer.write(hexString.replaceFirst('#', '')); 8 9 : return Color(int.parse(buffer.toString(), radix: 16)); 9 : } 10 : 11 15 : String toHex() => '${alpha.toRadixString(16).padLeft(2, '0')}' 12 9 : '${red.toRadixString(16).padLeft(2, '0')}' 13 9 : '${green.toRadixString(16).padLeft(2, '0')}' 14 9 : '${blue.toRadixString(16).padLeft(2, '0')}'; 15 : 16 3 : static bool isHexColor(String hexString) { 17 3 : RegExp hexColor = RegExp(r'(^((0x){0,1}|#{0,1})([0-9A-F]{8}|[0-9A-F]{6})$)'); 18 3 : return hexColor.hasMatch(hexString); 19 : } 20 : }