【发布时间】:2022-01-16 23:40:17
【问题描述】:
如果 API 响应为 HEx 代码 "ProductColor": "#20d6ee",我可以成功转换 API 颜色值,我在我的应用程序中通过以下方式将其转换为真实颜色:
color: Color( int?.parse(colorListListVar.productColor.replaceAll('#','0xff') ?? "",), ),
colorListListVar.productColor 我的 API 响应值在哪里。
但是硬编码颜色(白色,黑色等)中的一些 API 颜色值。这导致我的应用程序崩溃。
如果颜色值WHITE / BLACK 响应,我如何也显示真实颜色
API 响应:
{
"Status": 1,
"Message": "",
"ProductList": [
{
"CategoryId": "11",
"CategoryName": "FURNITURES",
"SubCategoryId": "38",
"SubCategoryName": "Lamp Shades",
"ColorList": [
{
"ProductId": "10363",
"ProductColor": "#20d6ee"
}
]
},
{
"CategoryId": "11",
"CategoryName": "FURNITURES",
"SubCategoryId": "38",
"SubCategoryName": "Lamp Shades",
"ColorList": [
{
"ProductId": "10362",
"ProductColor": "WHITE"
}
]
},
]
}
用户界面代码:
child: Row(
children: [
for (var colorListListVar in snapshot.data.productList[index].colorList)
Container(
margin: EdgeInsets.only(right: 2),
padding: EdgeInsets.all(getProportionateScreenWidth(8)),
height:getProportionateScreenWidth(40),
width:getProportionateScreenWidth(40),
decoration: BoxDecoration(
color: Colors.transparent,
//border: Border.all(color: kPrimaryColor),
shape: BoxShape.circle,
),
child: DecoratedBox(
decoration: BoxDecoration(
color: Color( int?.parse(colorListListVar.productColor.replaceAll('#','0xff') ?? "",), ),
shape:BoxShape.circle,
),
),
),
Spacer(),
],
),
【问题讨论】: