在应用程序的第一个版本中,Fflight中的提供者不会返回.在我的应用程序中,你可以 Select 应用程序的原色.例如,当我将其设置为 colored颜色 2(int表示一种 colored颜色 )时,我使用共享首选项保存它,并使用Provider在每个页面和小部件中使用它.很简单!但在该应用程序的第一个版本中,它不能获取 colored颜色 .更好地理解图像:
APP启动时:
当我使用导航栏时:
希望你能理解使用图片的问题.有没有人可以帮忙在应用程序第一次构建时立即获取 colored颜色 .
这是 colored颜色 切换的提供程序:
class ColorProvider with ChangeNotifier {
int _selectedColor = 0;
Color _mainColor = Colors.blue;
ColorProvider() {
_getColor();
}
int get selectedColor => _selectedColor;
set selectedColor(int index) {
_selectedColor = index;
_saveColor();
notifyListeners();
}
Color get mainColor => _mainColor;
Future<void> _saveColor() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setInt('_selectedColorKey', _selectedColor);
}
Future<void> _getColor() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
final savedSelectedColor = prefs.getInt('_selectedColorKey');
if (savedSelectedColor != null) {
_selectedColor = savedSelectedColor;
}
}
// This code can be used in another widget the provider and prefs only
// saves an int so with this you can choose (this isn't too important)
_getMainClr(int no) {
switch (no) {
case 0:
_mainColor = blueClr;
return blueClr;
case 1:
_mainColor = tifBlueClr;
return tifBlueClr;
case 2:
_mainColor = yellowClr;
return yellowClr;
default:
_mainColor = blueClr;
return blueClr;
}
}
}
我试图实现这段代码,以便它可以在开始时获取getPrefs
,但上面的color Provider
几乎已经做到了这一点,但这段代码值得一试.
@override
void initState() {
super.initState();
getPrefs();
}
Future<void> getPrefs() async {
//Get the int selected Color
setState(() {});
}