我有一个保存在共享首选项中的主题,该应用程序下次打开时应立即更新。我在main.dart中有一个ChangeNotifierProvider,它等待更新它,但是Wrapper.dart中出现错误:“在构建期间调用了setState()或markNeedsBuild()”。 我不确定如何在“包装器”中重新格式化我的代码以解决此问题。有什么想法吗?
Wrapper.dart
class Wrapper extends StatelessWidget {
static const id = "wrapper";
@override
Widget build(BuildContext context) {
final user = Provider.of<User>(context);
print(user);
bool checkedSharedPref = false;
if(checkedSharedPref == false){
SharedPref sharedPref = SharedPref();
ThemeOptions dropdownValue;
loadSharedPrefs() async {
try {
ThemeOptions currentTheme = ThemeOptions.fromJson(
await sharedPref.readThemeFromSharedPref('currentTheme'));
print("is being copyed");
dropdownValue = currentTheme;
} catch(e){
print(e);
}
}
checkedSharedPref = true;
loadSharedPrefs();
Provider.of<ThemeModel>(context, listen: false).toggleTheme();
}
return MainScreen();
}
MainScreen.dart
class MainScreen extends StatefulWidget {
static const id = "main_screen";
@override
_MainScreenState createState() => _MainScreenState();
}
class ThemeOptions{
Color themeColor;
ThemeType enumTheme;
ThemeOptions({this.themeColor, this.enumTheme});
ThemeOptions.fromJson(Map<String, dynamic> json)
: themeColor = Color(json['themeColor']),
enumTheme = json['enumTheme'];
Map<String, dynamic> toJson()=>{
'themeColor' : themeColor.toString(),
'enumType' : EnumToString.parse(enumTheme),
};
}
class SharedPref{
saveThemeToSharedPref(String key, ThemeOptions value) async{
final prefs = await SharedPreferences.getInstance();
prefs.setString(key, json.encode(value.toJson()));
print('dataSaved');
print(prefs.getString(key));
}
readThemeFromSharedPref(String key) async{
final prefs = await SharedPreferences.getInstance();
print('dataLoaded');
return json.decode((prefs.getString(key)));
}
}
class _MainScreenState extends State<MainScreen> {
ThemeOptions dropdownValue;
SharedPref sharedPref = SharedPref();
List<ThemeOptions> themes = [
ThemeOptions(themeColor: Colors.teal, enumTheme: ThemeType.Teal),
ThemeOptions(themeColor: Colors.green, enumTheme: ThemeType.Green),
/* ...
the other cases
...*/
];
loadSharedPrefs() async {
try{
ThemeOptions currentTheme = ThemeOptions.fromJson(await sharedPref.readThemeFromSharedPref('currentTheme'));
setState(() {
print("is being copyed");
dropdownValue = currentTheme;
});
}catch(e){
print(e);
}
}
@override
void initState() {
SharedPref sharedPref = SharedPref();
dropdownValue =themes[0];
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('MainScreen'),
),
body: Column(
children: <Widget>[
Container(
child: DropdownButton<ThemeOptions>(
value: dropdownValue,
icon: Icon(Icons.arrow_downward),
iconSize: 24,
elevation: 16,
style: TextStyle(
color: Colors.deepPurple
),
underline: Container(
height: 0.0,
color: Colors.deepPurpleAccent,
),
onChanged: (ThemeOptions newValue) {
setState(() {
dropdownValue = newValue;
sharedPref.saveThemeToSharedPref("currentTheme", dropdownValue);
Provider.of<ThemeModel>(context, listen: false).changeEnumValue(dropdownValue.enumTheme);
});
},
items: themes.map((ThemeOptions colorThemeInstance) {
return DropdownMenuItem<ThemeOptions>(
value: colorThemeInstance,
child: CircleAvatar(
backgroundColor: colorThemeInstance.themeColor,
),
);
})
.toList(),
),
),
SizedBox(height: 20.0,),
],
),
);
}
}
Themes.dart
class ThemeModel extends ChangeNotifier {
ThemeData currentTheme = tealTheme;
ThemeType _themeType = ThemeType.Teal;
toggleTheme() {
if (_themeType == ThemeType.Teal) {
currentTheme = tealTheme;
_themeType = ThemeType.Teal;
print('Teal');
notifyListeners();
}
/* ...
the other cases
...*/
}
ThemeType getEnumValue(){
return _themeType;
}
void changeEnumValue(ThemeType newThemeType){
_themeType = newThemeType;
toggleTheme();
}
}