Flutter provides color class to use only integer value and RGB
value but to use hash hexadecimal color code in flutter we need to add extra efforts so that we can use hash based hexadecimal string in flutter. Hexadecimal colors are those colors which starts with #
char.
Flutter Color class creates color using the integer
and we can create hash based color for black as follow
const Color(0xff000000); //#000000
const Color(0xff7f1c1c); //#7f1c1c
Creating by this above method can be difficult every time. So in this article we will also learn to use simple way to use Hash Hexadecimal Color Code in flutter with two other ways too. One is using the extension and other one is using the class by extending the base color class.
Method 2: Using the Extended Class to Use Hash Hexadecimal Color Code
In this method we will use a class and extend the base color class as follow
class HexColor extends Color {
static int _getColorFromHex(String hexColor) {
hexColor = hexColor.toUpperCase().replaceAll("#", "");
if (hexColor.length == 6) {
hexColor = "FF" + hexColor;
}
return int.parse(hexColor, radix: 16);
}
HexColor(final String hexColor) : super(_getColorFromHex(hexColor));
}
Now we can use it as below simply
Color color = HexColor("#7f1c1c");
Let’s understand hash hexadecimal color code in flutter with example
Step 1 : Create flutter project
Very first step is to create the fluter application using command line tool or in Android studio.
flutter create example_app
This will create a new project with name my_app then go in to the folder .
Step 2 : Create a class and extend color class
Next step is create the new class and extend the color class
import 'package:flutter/material.dart';
class HexColor extends Color {
static int _getColorFromHex(String hexColor) {
hexColor = hexColor.toUpperCase().replaceAll("#", "");
if (hexColor.length == 6) {
hexColor = "FF" + hexColor;
}
return int.parse(hexColor, radix: 16);
}
HexColor(final String hexColor) : super(_getColorFromHex(hexColor));
}
Step 3 : Create a simple widget
Now, Create a simple stateless widget to add the color to container and any widget. So for this we will create a stateless widget named as RedContainer
File : lib/RedContainer.dart
import 'package:example_app/classes/HexColor.dart';
import 'package:flutter/material.dart';
class RedContainer extends StatelessWidget {
const RedContainer({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
// TODO: implement build
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: const Text('Checkbox Example '),
),
body: Center(
child: Container(
padding: EdgeInsets.all(10),
color: HexColor("#000000"),
child: Text(
"Simple Hash Color",
style: TextStyle(color: HexColor("#CB4335")),
),
)),
));
}
}
Step 4: Import and use the widget in main
We just created a new widget with name RedContainer
, now we are going to use it in our main.dart file as below
import 'package:flutter/material.dart';
import 'package:example_app/RedContainer.dart';;
void main() {
runApp(const RedContainer());
}
Step 5: Run the project
Simply run the project using command line or in android studio to check the implementation.
flutter run lib/main.dart
Live Preview and code:
Also Read : How to Create Checkbox in Flutter?