How can I convert a flutter Color
class instance into a hex string?
For example, I would like to convert Colors.blue
to what would be '#4286f4'
.
Usecase is letting the user choose a color and save it in the database as a hex color.
I have checked related questions and they are for converting the other way around.
Step 1: Remove the # sign. Step 2: Add 0xFF at the beginning of the color code. Step 3: Place it inside the Color class like this Color(0xFFbfeb91).
Color color = new Color(0x12345678); String colorString = color. toString(); // Color(0x12345678) String valueString = colorString.
Material Color picker is a Flutter widget, that can be customizable. By default, it's Material Colors, but you can define your own colors. You can also use CircleColor widget to display color in your app. Example, you can set the color picker in a dialog and display the selected color in a ListTile, for settings.
You can convert the value
property (includes alpha) or the individual red
, green
, and blue
properties to Hex using int.toRadixString(16)
:
var myColor = Colors.blue; var hex = '#${myColor.value.toRadixString(16)}';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With