Hexa string color extension

Flutter tips Published on

Making Colors from Hex Strings

When you're building a Flutter app, you often get color codes as text, like "#FF0000" for red. But Flutter uses Color objects, not text strings, for colors. You need a way to change that text string into a Color that Flutter understands.

This little piece of code helps you do just that in a clean way.

The HexColor Extension

By using an 'extension', we can add a new function directly to the Color type. This makes it super easy to use later.

The extension is called HexColor and it works 'on Color'.

How the Code Works

Inside the HexColor extension, we have a special function called fromHex. It takes your hexadecimal color string (like "#RRGGBB" or "RRGGBB") as input.

extension HexColor on Color {
  static Color fromHex(String hexString) {
    final buffer = StringBuffer();
    if (hexString.length == 6 || hexString.length == 7)
      buffer.write('ff');
    buffer.write(hexString.replaceFirst('#', ''));
    return Color(int.parse(buffer.toString(), radix: 16));
  }
}
Steps Explained
  1. It first checks if your string is 6 or 7 characters long (like "RRGGBB" or "#RRGGBB").
  2. If it is, it adds 'ff' to the start of a temporary text builder. This 'ff' means the color is fully opaque (not transparent).
  3. It removes any '#' symbol from the start of your hex string.
  4. It adds the rest of your hex string (RRGGBB) to the temporary text builder.
  5. Finally, it takes the complete string (which now looks like "ffRRGGBB"), turns it into a number that represents the color, and creates a Flutter Color object from it.

Why This Is Useful

Instead of writing complicated code every time you need to convert a hex string, you can just use this extension. It makes your code cleaner and easier to read.

Save 3 months of work

Create your app using our 6 years of making Flutter apps and more than 50+ apps

kickstarter for flutter apps

Frequently Asked Questions

What is a Hex string color extension?

It's a piece of code that adds a helpful function (`fromHex`) to the built-in `Color` type in Flutter, allowing you to easily create a Color object from a text string containing a hexadecimal color code.

How do I use the `fromHex` function?

Once you add this extension code to your project, you can call it like this: `Color color = Color.fromHex('#1A2B3C');`

Does it work with hex codes that don't have a '#'?

Yes, the code removes the '#' if it's present, so it works for both

Does it handle the alpha value?

The provided code automatically adds 'ff' for the alpha value if the input string is 6 or 7 characters long, making the color fully opaque.

Read more
You may also be interested in
Delegate widget design  blog card image
Delegate widget design
Published on 2025-05-12T09:03:59.853Z
Compare app versions  blog card image
Compare app versions
Published on 2025-05-12T09:22:19.759Z
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved