How to convert API color code ("#1db8ce") value to Actual RGB color?

0

Issue

I’m Getting all my data from Api. I want to convert the Color string value to Actual color. How can I achieve that?

My API response:

{
"Status": 1,
"Message": "",
"ProductList": [
    {
        "CategoryId": "2",
        "CategoryName": "Women",
        "SubCategoryId": "14",
        "SubCategoryName": "Nighty",
        "DiscountStartDate": "",
        "DiscountEndDate": "",
        "Tag": "Nighty",
       
     
        "ColorList": [
            {
                "ProductId": "52",
                "ProductColor": "#1db8ce"
            },
            {
                "ProductId": "52",
                "ProductColor": "#8ab234"
            }
        ]
    },
   ...................................................

I’m trying to display those 2 color, which is coming from my API in a Row(). For now I’m getting 2 Red color box (as I set the Color Red manually).

 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: Colors.red, //I want to use like this ==> colorListListVar.ProductColor
                                                      shape:BoxShape.circle,
                                                    ),
                                                  ),
                                                ),
                                              Spacer(),
                                            ],
                                          ),

Solution

You can easily achieve it this way:

String color = '#1db8ce';
Color actualColor = Color(int.parse(color.replaceAll('#','0xff')));

Answered By – esentis

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More