What Are Flutter Colors?
Flutter uses the Color class to represent a color. It belongs to dart:ui, rather than being specific to Material Design.
The Colors class supplies predefined Material constants such as Colors.red, Colors.blue and Colors.green.
Color Formats and Material Swatches
- ARGB channels:
Color.fromARGB(a, r, g, b)accepts four integer channels from 0 to 255. Alpha controls opacity; the other channels are red, green and blue. - Hexadecimal notation:
Color(0xAARRGGBB)expresses the same channels as an integer literal; HEX is notation, not a separate color model. - Material swatches: A
MaterialColorprovides a set of shades. For app-wide roles, obtain colors fromTheme.of(context).colorScheme.
Flutter Code for Colors
This example uses a Material color constant for the app bar and an opaque orange HEX value for the body.
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Flutter Colors Example'),
backgroundColor: Colors.blue,
foregroundColor: Colors.black,
),
body: const ColoredBox(
color: Color(0xFFFF6F00),
child: Center(
child: Text(
'Hello, Flutter!',
style: TextStyle(fontSize: 24, color: Colors.black),
),
),
),
),
);
}
}Color(0xFFFF6F00) sets the orange background. Color.fromARGB(255, 255, 111, 0) represents the same color.
Four Material Color Examples
These four examples use predefined colors from Flutter. Each sets the text color explicitly.
1. Red
Red can mark an error state. Include an error message so color is not the only signal.
Container(
color: Colors.red,
child: const Text('Red Color', style: TextStyle(color: Colors.black)),
);2. Blue
Use a blue surface for this example; the text color is set explicitly.
Container(
color: Colors.blue,
child: const Text('Blue Color', style: TextStyle(color: Colors.black)),
);3. Green
A green confirmation can be paired with a short label describing what succeeded.
Container(
color: Colors.green,
child: const Text('Green Color', style: TextStyle(color: Colors.black)),
);4. Yellow
Black lettering is used on the yellow example rather than leaving the text color to the surrounding theme.
Container(
color: Colors.yellow,
child: const Text('Yellow Color', style: TextStyle(color: Colors.black)),
);Flutter: How to work with hexadecimal colors?
A six-digit web color such as #FF6F00 contains RGB channels. For Flutter’s integer constructor, put the alpha byte first: 0xFFFF6F00.
AA: opacity, from00(transparent) toFF(opaque).RR,GG,BB: red, green and blue.
0x marks a hexadecimal integer in Dart. Omitting the alpha byte can make the color fully transparent.
Hexadecimal Example in Flutter:
Container(
color: const Color(0xFF42A5F5),
child: const Text('Hex Color Example', style: TextStyle(color: Colors.black)),
);Creating a Flutter Color Tool
A small converter can accept a six-digit #RRGGBB value and add the opaque alpha byte. Define the accepted format before parsing: the example below deliberately accepts six digits, with or without #.
While someone edits the field, keep the last valid preview and show a validation message for incomplete or invalid input.
Flutter Code for Basic Color Conversion:
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: ColorConverter()));
Color? parseHexColor(String input) {
final hex = input.trim().replaceFirst(RegExp(r'^#'), '');
if (!RegExp(r'^[0-9a-fA-F]{6}$').hasMatch(hex)) return null;
final rgb = int.tryParse(hex, radix: 16);
return rgb == null ? null : Color(0xFF000000 | rgb);
}
class ColorConverter extends StatefulWidget {
const ColorConverter({super.key});
@override
State<ColorConverter> createState() => _ColorConverterState();
}
class _ColorConverterState extends State<ColorConverter> {
Color preview = const Color(0xFF42A5F5);
String? error;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Flutter Color Converter')),
body: Padding(
padding: const EdgeInsets.all(16),
child: Column(
children: [
TextFormField(
initialValue: '#42A5F5',
decoration: InputDecoration(
labelText: 'HEX color (#RRGGBB)',
errorText: error,
),
onChanged: (value) {
final parsed = parseHexColor(value);
setState(() {
error = parsed == null ? 'Enter six HEX digits.' : null;
if (parsed != null) preview = parsed;
});
},
),
const SizedBox(height: 16),
Container(width: 100, height: 100, color: preview),
],
),
),
);
}
}#42A5F5 and 42a5f5 both produce 0xFF42A5F5. Blank input, non-HEX characters and three- or eight-digit values are rejected by this six-digit parser.
Last Words
Use a color literal when you need one exact value, and theme roles when widgets should follow the app’s color scheme. Keep the input format explicit when building a converter.
This article is credited to Purrweb, a Flutter application development company.
