Flutter/Dart: Convert String to Color?
1,966
Actually, I wrote a code that maps Strings to Colors
Map<String,Color>
Author by
Bashar ALAYAN
Updated on December 18, 2022Comments
-
Bashar ALAYAN 11 months
My database has colors saved as a String like
"black"
and so I'm basically looking for a function like this:Color.FromName("black")
How can I convert to Color in this situation?-
jamesdlin over 3 yearsI have no experience with it, but it seems that package:color might do what you want. Otherwise it shouldn't be too hard to write a script to scan through Flutter's
colors.dart
and to generate code that mapsString
s toColor
s. -
thusith.92 over 3 yearsexpanding on @jamesdlin's comment, package:color has the rgb methods that will match your requirement.
RgbColor black = new RgbColor.name('black');
Make sure your DB has the proper CSS color names. rapidtables.com/web/css/css-color.html -
Yauhen Sampir over 3 yearsmaybe you can just create an extension on color class and put all colors from db to extension?
-
Bashar ALAYAN over 3 years@jamesdlin Yes I will write a script to generate code that maps Strings to Colors.
-
Bashar ALAYAN over 3 years@thusith.92 I tried to do this :
RgbColor black = new RgbColor.name('black');
but it's not working because the packages Painting.dart and Color.dart
-