I can successfully Convert API color value if the API response in HEx code "ProductColor": "#20d6ee"
which I convert in real color in my App following way:
color: Color( int?.parse(colorListListVar.productColor.replaceAll('#','0xff') ?? "",), ),
where colorListListVar.productColor
my API response value.
But some API Color value in Hard Coded color(White,Black .etc) . Which is causing my App crashes.
How can I also show the real color if Color value WHITE
/ BLACK
in response
API RESPONSE:
{
"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"
}
]
},
]
}
UI CODE:
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(),
],
),
CodePudding user response:
Well for this I'll suggest you to ask your backend developer to put single(hex)
format response not color name
but for this scenario you can check them using if else
or ternary operator
if(colorListListVar.productColor == "white") {
return Colors.white
}
colorListListVar.productColor == "white" ? Colors.white : Color( int?.parse(colorListListVar.productColor.replaceAll('#','0xff') ?? "",), )
CodePudding user response:
You can just create a Color object with that hex values for instance for the value "#20d6ee" you can do Color(0xff20d6ee)