Norbert515 / dynamic_theme

Dynamically changing your theme without hassle
MIT License
322 stars 68 forks source link

Load saved theme on start #27

Open mliitfall opened 5 years ago

mliitfall commented 5 years ago

Hi, love this package. I don't know how to load saved theme in the first start in MaterialApp? on data, It going to _loadSavedThemeData and based on SharedPreferences local storage to get String then if case is blue, it will return blueTheme. But what I got is sometimes it could load the theme, sometimes it could not load. Could you help me?

DynamicTheme(
          defaultBrightness: Brightness.light,
          data: (brightness) => _loadSavedThemeData,
          themedWidgetBuilder: (context, theme) {
            return MaterialApp(
              debugShowCheckedModeBanner: false,
//        title: AppLocalizations.of(context).tr('title'),
              theme: theme...
);

_loadSavedThemeData() {
    switch(Pref.localStorage.getString(Strings.themes)){
      case 'blue':
        return CustomTheme.blueTheme;
      case 'green':
        return CustomTheme.greenTheme;
    }
  }

class CustomTheme {
  static final ThemeData blueTheme = ThemeData(
    backgroundColor: Colors.blue,
    accentColor: Colors.blue[800],
    primaryColor: Colors.black,
  );
}
Norbert515 commented 5 years ago

That seems weird, are you sure your Pref class is working as expected? Just looked over the code, and this should in fact work.