Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter convert Color to hex string

Tags:

flutter

dart

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.

like image 431
Joel Hernandez Avatar asked Mar 13 '19 17:03

Joel Hernandez


People also ask

How do you get hex value from color in Flutter?

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).

How do you turn a color into a String in Flutter?

Color color = new Color(0x12345678); String colorString = color. toString(); // Color(0x12345678) String valueString = colorString.

How do you select colors in Flutter?

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.


1 Answers

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)}'; 
like image 66
Günter Zöchbauer Avatar answered Sep 29 '22 12:09

Günter Zöchbauer