优先使用颤动来节省色彩 [英] Sharedpreferencs with flutter to save color
问题描述
我希望容器是一种颜色,它来自sharedpreference或类似的东西!是否有一个小部件来解决这个问题!或者我可以使用sharedpreference来做到这一点!!如果我能!如何?
I want a container to be of a color, that comes from sharedpreference or something like that! Is there a widget to solve this!! Or can I just do this with sharedpreference!! If I can! How?
推荐答案
随风飘扬的 SharedPreferences 插件,您可以只能保存String, int, StringList, double, Bool
.
With flutter SharedPreferences plugin you can only save String, int, StringList, double, Bool
.
一种解决方法是将颜色的RGBO
值保存在SharedPreferences
中,这将适用于Android和iOS.
A work around would be to save the RGBO
value of the color in SharedPreferences
, this would work for Android and iOS.
第1步.安装插件
pubspec.yaml
将SharedPreferences添加到您的pubspec.yaml
文件.通过点击此处来检查最新版本.
Add SharedPreferences to your pubspec.yaml
file. Check the last version by clicking here.
dependencies:
flutter:
sdk: flutter
shared_preferences: ^0.5.3+4
第2步.将RGBO值保存在SharedPreferences中
void saveColor(int r, int g, int b, double opacity) async {
final prefs = await SharedPreferences.getInstance();
prefs.setInt('r', r);
prefs.setInt('g', g);
prefs.setInt('b', b);
prefs.setDouble('o', opacity);
}
第3步.获取值并创建颜色
getColor() async {
final prefs = await SharedPreferences.getInstance();
final r = prefs.getInt('r');
final g = prefs.getInt('g');
final b = prefs.getInt('b');
final opacity = prefs.getDouble('o');
return Color.fromRGBO(r, g, b, opacity);
}
这篇关于优先使用颤动来节省色彩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!