Flutter/Dart: Convert String to Color?

1,966

Actually, I wrote a code that maps Strings to Colors

Map<String,Color>
Share:
1,966
Bashar ALAYAN
Author by

Bashar ALAYAN

Updated on December 18, 2022

Comments

  • Bashar ALAYAN
    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
      jamesdlin over 3 years
      I 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 maps Strings to Colors.
    • thusith.92
      thusith.92 over 3 years
      expanding 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
      Yauhen Sampir over 3 years
      maybe you can just create an extension on color class and put all colors from db to extension?
    • Bashar ALAYAN
      Bashar ALAYAN over 3 years
      @jamesdlin Yes I will write a script to generate code that maps Strings to Colors.
    • Bashar ALAYAN
      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