is there a way to (set and get) Color data type or a List of Widgets data type by using shared preference package
i want to save the Color that the user has choose as well as the widget. so when the user closes the app and rerun it again the app still show the color and the widgets.
thank you
Yes you can.
In order to save a value to disk the value must be in a type that shared preferences can use and write to disk.
For this example we will focus on the color property, convert it to a json and encode it as a string.
var color = Colors.white;
var json = {
'r': color.r,
'g': color.g,
'b': color.b,
'a': color.a
};
var serializedValue = json.encode(json);
await SharedPreferences.setString('<your unique key>', serializedValue);
Then you can use serialized value with shared preferences setString
, and when you retrieve the string reverse the encoding above to create a Color
type
You will need to perform this conversion for each property of the Widget
in order to save and re-create it. Above I have outlined to process for the color property, you will need to do the same for the others.
You will not be able to directly save the widget to disk, as it must first be serialized to a format that can be written to disk