[英]Flutter/Dart: Convert String to Color?
我的数据库将颜色保存为像"black"
这样的字符串,所以我基本上是在寻找这样的函数: Color.FromName("black")
在这种情况下我如何转换为颜色?
实际上,我写了一个将字符串映射到颜色的代码
Map<String,Color>
I ran it for hexadecimal colors.
{
Category category = categoryList[index];
String hexString = category.color; // string value e.g: 'EF9A9A' from database
int myColor = int.parse("0xff$hexString");
return Card(
...
color: Color(myColor),
}
class Category {
String id;
String title;
String color;
Category({
this.id,
this.title,
this.color,
});
factory Category.fromJson(Map<String, dynamic> json) => Category(
id: json["id"] ,
title: json["title"],
color: json["color"],
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.