How to convert Flutter color to string and back to a color

前端 未结 4 1626
小蘑菇
小蘑菇 2020-12-06 02:23

I am converting a Color to a String. I am then converting the Color to a String. Unfortunately when I want to convert it back into a Color the operation fails:

<         


        
4条回答
  •  天命终不由人
    2020-12-06 03:07

    You actually can't do that. Color doesn't have a constructor that accepts a String as a representation of a color.

    For that, you could use the Color property value. It is a 32 bit int value that represents your color. You can save it and then use to create your new Color object.

    The code could look like this

    Color pickerColor = new Color(0xff443a49);
    int testingColorValue = pickerColor.value;
    String testingColorString = pickerColor.toString();
    
    Color newColor = new Color(testingColorValue);
    

    or like this

    Color pickerColor = new Color(0xff443a49);
    String testingColorString = pickerColor.toString();
    
    Color newColor = new Color(pickerColor.value);
    

提交回复
热议问题