How do I use hexadecimal color strings in Flutter?

CodeWithFlutter
2 Min Read

In Flutter, you can use hexadecimal color strings to specify colors in your UI. Hexadecimal color strings are strings that represent colors in hexadecimal format, such as #FF0000 for red or #00FF00 for green.

Here’s an example of how to use a hexadecimal color string in Flutter:

Container(
  color: Color(0xFF0000FF), // blue
  child: Text('Hello World'),
),

In this example, the Color constructor is used to create a Color object from the hexadecimal color string 0xFF0000FF, which represents blue. The color property of the Container widget is then set to this Color object.

Note that the 0x prefix is used to indicate that the following string is a hexadecimal number. The first two characters represent the red channel, the next two represent the green channel, and the last two represent the blue channel.

You can also use the Color method fromARGB to create a Color object from separate values for the alpha, red, green, and blue channels:

Color color = Color.fromARGB(255, 255, 0, 0); // red

This method allows you to specify the color channels as separate values, rather than as a single hexadecimal string.

Share this Article
Leave a comment