As the [Color][1] constructor does not support **hexadecimal string**, so we should find other alternatives.
**There are several possibilities:**
**1-** The first one is to create a small function that will allow you ***to convert a color hex-string to a Color object***.
**Code:**
Color colorFromHex(String hexColor) {
final hexCode = hexColor.replaceAll('#', '');
if (hexColor.length == 6) {
hexColor = 'FF' + hexColor; // FF as the opacity value if you don't add it.
}
return Color(int.parse('FF$hexCode', radix: 16));
}
**Usage**:
Container(
color: colorFromHex('abcdff'),
child: Text(
'Never stop learning',
style: TextStyle(color: colorFromHex('bbffffcc')),
),
)
**2-** The second possibility is to use the **[supercharged][2] package.** [Supercharged][2] brings all the comfort features from languages like Kotlin to all Flutter developers.
Add the dependency supercharged: `^1.X.X` (find recent version) to your project and start using Supercharged everywhere:
import 'package:supercharged/supercharged.dart';
Now ,transform any String to colors
**Code :**
"#ff00ff".toColor(); // Painless hex to color
"red".toColor(); // Supports all web color names
You can also use the **[hexcolor][3]** package which is also great.
[1]:
[To see links please register here]
[2]:
[To see links please register here]
[3]:
[To see links please register here]