How do I use hexadecimal color strings in Flutter

0 votes

How do I convert a hexadecimal color string like #b74093 to a Color in Flutter?

I want to use a HEX color code in Dart.

Mar 1, 2023 in Android by Ashwini
• 5,430 points
12,816 views

1 answer to this question.

0 votes

To use a hexadecimal color string in Flutter, you can simply create a new instance of the Color class and pass in the hexadecimal value as a string preceded by the pound sign (#).

Here's an example of how to convert a hexadecimal color string to a Color in Flutter:

String hexColor = "#b74093";
Color color = Color(int.parse(hexColor.substring(1, 7), radix: 16) + 0xFF000000);

In the example above, we first define the hexadecimal color string as hexColor. We then convert the string to an integer using the int.parse() method and passing in the substring of the string (excluding the pound sign) and the radix (base 16 for hexadecimal). We add 0xFF000000 to set the alpha value to 255 (fully opaque) since the Color constructor requires an alpha value.

Now you can use the color variable wherever you need to set a color in your Flutter app.

Container(
  color: color,
  child: Text("Hello World"),
),

This will create a Container widget with a background color of #b74093.

To know more about Flutter, join our Flutter Certification today.

answered Mar 1, 2023 by vishalini

Related Questions In Android

0 votes
1 answer

How do I disable a Button in Flutter?

To disable a button in Flutter, you ...READ MORE

answered Mar 10, 2023 in Android by vinayak
14,623 views
0 votes
1 answer

How do I Set Background image in Flutter?

To set a background image in Flutter, ...READ MORE

answered Mar 10, 2023 in Android by vinayak
39,195 views
0 votes
0 answers

How do I turn a C# object into a JSON string in .NET?

I have classes like these: class MyDate { ...READ MORE

Sep 20, 2022 in Android by Edureka
• 13,620 points
449 views
0 votes
0 answers

How do I open any app from my web browser (Chrome) in Android?

I want to use my web browser ...READ MORE

Nov 16, 2022 in Android by Edureka
• 12,690 points
549 views
0 votes
1 answer

Dart_LoadScriptFromKernel: The binary program does not contain 'main'.

Hi@akhtar, You need to add the main() function ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,460 points
3,530 views
0 votes
1 answer

How to install Dart in Windows system?

Hi@akhtar, Dart is the programming language used to code Flutter apps. ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,460 points
1,089 views
0 votes
1 answer

flutter run: No connected devices

Hi@akhtar, To prepare to run and test your ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,460 points
4,106 views
0 votes
1 answer

How to create a function in Dart language?

Hi@akhtar, There are many function types available in ...READ MORE

answered Jul 22, 2020 in Others by MD
• 95,460 points
740 views
0 votes
1 answer

How can I add a border to a widget in Flutter?

To add a border to a widget ...READ MORE

answered Mar 1, 2023 in Android by vishalini
11,344 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP