Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to Use Hash Hexadecimal Color Code in flutter

How to Use Hash Hexadecimal Color Code in flutter ?

Aman Jain, September 13, 2022March 16, 2024

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?

Related

Flutter Dart colorFlutterHexaDecimal

Post navigation

Previous post
Next post

Related Posts

Flutter Display background image in Flutter

How to display background image in flutter ?

January 15, 2022August 29, 2022

In our recent article i showed you to display local or remote image in flutter application but in some cases we need to show our image in background so we can put additional content on it. Background image is useful to show when we wanted to show full image in…

Read More
Dart How to Create Checkbox in Flutter

How to Create Checkbox in Flutter?

September 12, 2022March 16, 2024

In this tutorial we will learn to create checkbox in flutter application. Checkbox is used to provide multiple options to users and they can select multiple value for same input. Usage of checkbox input can be for remember me, check terms and condition, multiple answers questions choices and etc. Flutter…

Read More
Uncategorized Flutter http request to call api

Simple way to call API in flutter using http package

January 11, 2022January 15, 2022

Every client side application require connection to server to share the data and get most updated data from server. In the same way flutter also require to call the APIs’ and get data from server and also store the data in server. So in this article i will show you…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version