Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

flutter

dart

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:

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

   Color newColor;

   newColor = testingColorString as Color;

type 'String' is not a subtype of type 'Color' in type cast where String is from dart:core Color is from dart:ui

like image 762
Chrystian Avatar asked Apr 14 '18 19:04

Chrystian


People also ask

How do you turn a string into a color in Flutter?

And since this object is a String, you can't change it back to a Color with an as. Instead, you can parse the string into a value and construct a new Color object. Color color = new Color(0x12345678); String colorString = color. toString(); // Color(0x12345678) String valueString = colorString.

How do you save a color in Flutter?

Saving: final Color myColor = Colors. deepPurple[200]; final red = myColor. red; print(red); // 179 final green = myColor.

How do I change text color dynamically in Flutter?

Edit: if you want different color for every item, u can use function like this, Color getColor(number) { if (number > 0 && number < 100) return Colors. red; if (number >= 100 && number < 200) return Colors.


2 Answers

In Dart the as operator doesn't allow you to change the actual structure of an Object, it just allows you to provide a hint that an object might have a more specific type. For example, if you had a dog and an animal class you could use as to specify that your animal is actually a dog (as long as the object is actually a dog).

class Animal {}
class Dog extends Animal {}

Animal animal = new Dog();
Dog bob = animal as Dog; // works, since animal is actually a dog
Animal animal2 = new Animal();
Dog bob2 = animal2 as Dog; // fails, since animal2 is actually an Animal

Now, in the example you've provided toString actually just creates a String representation of the current Color value. And since this object is a String, you can't change it back to a Color with an as. Instead, you can parse the String into a value and construct a new Color object.

Color color = new Color(0x12345678);
String colorString = color.toString(); // Color(0x12345678)
String valueString = colorString.split('(0x')[1].split(')')[0]; // kind of hacky..
int value = int.parse(valueString, radix: 16);
Color otherColor = new Color(value);
like image 85
Jonah Williams Avatar answered Oct 28 '22 00:10

Jonah Williams


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);
like image 33
icazevedo Avatar answered Oct 28 '22 00:10

icazevedo