
Hello, Flutter developers!👋 Today I would like to explore an aspect which is often taken for granted as simple yet can have huge effects in terms of UI development and that’s handling HEX colors in Flutter. I remember that back in the day when I started working with Flutter, converting HEX values was something that took a lot of time- that is till I found the hexcolor package!
Why Use HEX Colors?
It is common when working with designers or taking over from previous web design team to receive project files with HEX colors (#FFFFFF for white, for example). Therefore it is very useful that Flutter uses Color objects as their primary color notation. Luckily hexcolor package simplifies the use of colors in the HEX format in the source code incredibly.
Setting Up the hexcolor Package
First things first, let's add the package to your project. Open your
pubspec.yaml
file and add:dependencies: hexcolor: ^3.0.1
-
Run this command in your terminal:
flutter pub get
Basic Usage
Now for the fun part! Here's how to use HEX colors in your Flutter app:
import 'package:hexcolor/hexcolor.dart';
// In your widget
Container(
color: HexColor("#FFFFFF"), // White
child: Text("Hello, Flutter!"),
)
HexColor("#FFFFFF")
for RGB, or HexColor("0xFF0000")
for ARGB format.
Advanced Usage Examples
Let's look at some practical examples where hexcolor really shines:
// Using different HEX formats
final color1 = HexColor('#FF0000'); // Standard 6-digit
final color2 = HexColor('#80FF0000'); // With alpha channel
final color3 = HexColor('0xFF0000'); // Without #
// Creating a custom theme
ThemeData(
primaryColor: HexColor("#2196F3"),
accentColor: HexColor("#FFC107"),
backgroundColor: HexColor("#FAFAFA"),
)
Best Practices
When it comes to working with HEX colors in active services, I can offer some advice I have acquired over the years:
- Create a color constants file to organize all the colors in the Hex selection.
- Make efforts to pick out words related to the color and attach them as color variable names.
- Colors service related comments in the code for users easy crosschecking of them during reading or maintaining the code.
// colors.dart
import 'package:hexcolor/hexcolor.dart';
class AppColors {
// Primary colors
static final primary = HexColor("#2196F3"); // Blue
static final accent = HexColor("#FFC107"); // Amber
// Background colors
static final background = HexColor("#FAFAFA"); // Almost white
static final surface = HexColor("#FFFFFF"); // White
// Text colors
static final textPrimary = HexColor("#212121"); // Almost black
static final textSecondary = HexColor("#757575"); // Grey
}
Troubleshooting Common Issues
Sometimes you might run into these common challenges:
1. Invalid HEX Code Format
The color code format you entered is invalid. Please ensure that you use the correct format.
HexColor("FF0000"); // Missing #
When using an ARGB hex color code without a #
, you need to follow this structure:
final color3 = HexColor('0xFF0000'); // ARGB format (with 0x prefix)
Use this method only if your color code follows the ARGB format and starts with 0x
.
Alternatively, for standard RGB hex color codes with a #
, you can use:
final color1 = HexColor('#FF0000'); // RGB format (with # prefix)
You have used the correct format for the color code:
HexColor("#FF0000"); // With #
2. Alpha Channel Usage
Add transparency to your color code using the alpha channel. The value 80
represents 50% opacity:
// Adding transparency
// 80 is the alpha value (50% opacity)
HexColor("#80FF0000");
Conclusion
The hexcolor package will prove invaluable when it comes to HEX colors integration into your application. The more you play around with it, you’ll really appreciate how great it is, especially when it comes with dealing with a design team or converting web designs to Flutter apps. Just remember and always state your colours in the constants file and you will have an easily maintainable color system in your Flutter app for designers to work with!
Stay tuned and Happy Coding! If you found this guide useful, feel free to share it with other Flutter developers. Also, remember to refer to the official hexcolor package documentation