Hex to Flutter color converter: Color(0xFF…) from any hex code
Convert a hex, rgb() or hsl() color to Flutter's Color(0xAARRGGBB), fromARGB, fromRGBO and withValues, with the alpha in the right place, or a list to a palette.
To use a hex color in Flutter, put 0x in front of it, add the alpha as the first two digits and pass it to Color: #42A5F5 becomes const Color(0xFF42A5F5), where FF means fully opaque. Flutter reads the number as AARRGGBB, alpha first, while CSS writes #RRGGBBAA with alpha last, so an 8-digit CSS color has to be reordered, not just prefixed. Type any color below to get every Flutter form of it, ready to copy.
Why Flutter puts the alpha first
Color(int value) takes a 32-bit integer laid out as 0xAARRGGBB: bits 24–31 are alpha, then red, green and blue. That is the same order Android uses. CSS and design tools such as Figma export 8-digit hex as #RRGGBBAA, alpha last. If you copy #00897B80 (teal at 50%) and write Color(0x00897B80), Flutter reads alpha 00, so the color is invisible; the right constant is Color(0x8000897B). The tool shows the wrong reading next to the right one whenever you enter 8 digits, so you can see which one you have.
The same goes for 6 digits without an alpha. Color(0x42A5F5) compiles, but the missing top byte is zero, so the color is fully transparent. Always write all eight digits, starting with FF for an opaque color.
Color, fromARGB, fromRGBO and Color.from
All four make the same color; pick whichever matches the numbers you were given.
| Constructor | Takes | Use it when you have |
|---|---|---|
Color(0xFF42A5F5) | one 32-bit int, AARRGGBB | a hex code |
Color.fromARGB(255, 66, 165, 245) | alpha, red, green, blue as 0–255 | an alpha in 0–255, as in Android |
Color.fromRGBO(66, 165, 245, 1.0) | red, green, blue 0–255, opacity 0.0–1.0 | CSS rgba() values |
Color.from(alpha: 1.0, red: 0.259, …) | every channel as 0.0–1.0, plus an optional colorSpace | float values, or Display P3 colors |
All of them are const constructors, so a color written this way costs nothing at runtime. Since Flutter 3.27, colors store each channel as a double, which is why Color.from exists and why reading a channel is now color.r (0.0–1.0) instead of the deprecated color.red. To get the old 32-bit integer back, call toARGB32(); the value getter is deprecated too.
withOpacity is deprecated: use withValues
Color.withOpacity() is marked deprecated in current Flutter with the message to use .withValues() to avoid precision loss: it rounded the opacity to 8 bits, while the new color model keeps a double. Replace color.withOpacity(0.5) with color.withValues(alpha: 0.5). If the opacity never changes, bake it into the constant instead, using the alpha byte from this table:
| 100% | 90% | 80% | 70% | 60% | 50% | 40% | 30% | 20% | 10% | 0% |
|---|---|---|---|---|---|---|---|---|---|---|
FF | E6 | CC | B3 | 99 | 80 | 66 | 4D | 33 | 1A | 00 |
Parsing a hex string at runtime
Colors from an API or a config file arrive as strings. Flutter has no built-in hex parser, but a short extension does it. This one accepts the CSS forms, with or without #, and moves the alpha to the front:
Dart: HexColor extension
import 'dart:ui';
extension HexColor on Color {
/// Parses #RGB, #RGBA, #RRGGBB or #RRGGBBAA (CSS order, alpha last),
/// with or without the #. Throws a FormatException otherwise.
static Color fromHex(String hex) {
var h = hex.trim();
if (h.startsWith('#')) h = h.substring(1);
if (h.length == 3 || h.length == 4) {
h = h.split('').map((d) => '$d$d').join();
}
if (h.length == 6) h = '${h}ff';
if (h.length != 8) throw FormatException('Not a hex color', hex);
final rgba = int.parse(h, radix: 16);
// CSS puts alpha last, Flutter's Color(int) wants it first.
return Color(((rgba & 0xff) << 24) | (rgba >> 8));
}
/// #RRGGBB, or #RRGGBBAA when the color isn't opaque.
String toHex() {
final argb = toARGB32();
final rgb = (argb & 0xffffff).toRadixString(16).padLeft(6, '0');
final alpha = (argb >> 24) & 0xff;
return alpha == 0xff
? '#$rgb'
: '#$rgb${alpha.toRadixString(16).padLeft(2, '0')}';
}
}
// final brand = HexColor.fromHex('#42A5F5');
// brand.toHex(); // '#42a5f5'Call it as HexColor.fromHex('#42A5F5'): static members of an extension are called on the extension's name, not on Color. toHex() goes the other way, through toARGB32().
Turn a list of hex codes into a Dart palette
Paste one color per line, from a design token file, a Figma export or CSS variables. A name before or after the color becomes the field name; lines without one get numbered.
If the palette feeds a Material theme, pass the main brand color to ColorScheme.fromSeed and let it derive the rest, then override only the roles that must match the brand exactly, such as primary or error. For icons rather than flat colors, the SVG to Flutter converter turns an SVG into a CustomPainter with its colors already written as Color(0xAARRGGBB).
Sources
- Color class, including withValues and toARGB32, in the Flutter API docs
- ColorScheme.fromSeed in the Flutter API docs
- Migration guide for wide gamut Color (why
withOpacityandvaluewere deprecated) - CSS Color 4, hex notation (W3C)
Questions
How do I use a hex color code in Flutter?
Replace the # with 0xFF and pass it to Color: #42A5F5 becomes const Color(0xFF42A5F5). The FF is the alpha, meaning fully opaque.
Why is my Flutter color transparent or wrong?
Usually because the alpha is missing or in the wrong place. Color(0x42A5F5) has alpha 00 and is invisible, and an 8-digit CSS hex like #42A5F580 has its alpha last, so in Flutter it must be written Color(0x8042A5F5).
Is withOpacity deprecated in Flutter?
Yes. Since Flutter 3.27, Color.withOpacity is deprecated in favour of withValues(alpha: …), because colors now store each channel as a double and withOpacity rounded the opacity to 8 bits. color.withValues(alpha: 0.5) is the direct replacement.
What is the difference between Color.fromARGB and Color.fromRGBO?
Color.fromARGB takes alpha, red, green and blue as integers from 0 to 255. Color.fromRGBO takes red, green and blue as 0–255 and the opacity as a double from 0.0 to 1.0, like CSS rgba().
How do I convert a hex string to a Color at runtime in Flutter?
Strip the #, add ff for the alpha if there are only 6 digits, parse it with int.parse(h, radix: 16) and pass the result to Color, moving a CSS alpha from the end to the front. The HexColor extension on this page does exactly that.
How do I get the hex code back from a Flutter Color?
Call color.toARGB32() and format it with toRadixString(16).padLeft(8, '0'); that gives AARRGGBB. The older color.value getter does the same but is deprecated.
How do I create a Material 3 color scheme from one hex color?
Pass it as the seed: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xFF6750A4))). Flutter derives every role from it, and brightness: Brightness.dark gives the matching dark scheme.