title: How to store theme in shared preferences in flutter
body: <p>Here I've one screen where I've some button when I press the button it will change the color of application but it will reset when I restart the app. I want that it will not reset until I am changing the color from the App. so for that, I want to store THemedata in shared preferences and I want to get theme data from shared preferences so whenever I restart the app it needs to take the theme from Shared preferences.</p>
  
  <p>Here is some code I've tried,</p>
  
  <p><strong>ThemeBloc.dart</strong></p>
  
  <pre><code>class ThemeBloc extends Bloc&lt;ThemeEvent, ThemeState&gt; {
    @override
    ThemeState get initialState =&gt;
        ThemeState(themeData: appThemeData[AppTheme.GreenLight]);
  
    @override
    Stream&lt;ThemeState&gt; mapEventToState(
      ThemeEvent event,
    ) async* {
      if (event is ThemeChanged) {
        yield ThemeState(themeData: appThemeData[event.theme]);
      }
    }
  }
  </code></pre>
  
  <p><strong>ThemeEvent.dart</strong></p>
  
  <pre><code>abstract class ThemeEvent extends Equatable {
    ThemeEvent([List props = const &lt;dynamic&gt;[]]) : super(props);
  }
  
  class ThemeChanged extends ThemeEvent {
    final AppTheme theme;
  
    ThemeChanged({
      this.theme,
    }) : super([theme]);
  }
  </code></pre>
  
  <p><strong>ThemeState.dart</strong></p>
  
  <pre><code>@immutable
  class ThemeState extends Equatable {
    final ThemeData themeData;
  
    ThemeState({
      @required this.themeData,
    }) : super([themeData]);
  }
  </code></pre>
  
  <p><strong>AppTheme.dart</strong></p>
  
  <pre><code>enum AppTheme {
    GreenLight,
    GreenDark,
    BlueLight,
    BlueGrey,
    Amber,
  }
  
  final appThemeData = {
    AppTheme.GreenLight: ThemeData(
      brightness: Brightness.light,
      primaryColor: Colors.teal,
    ),
    AppTheme.GreenDark: ThemeData(
      brightness: Brightness.light,
      primaryColor: Colors.orange,
    ),
  };
  </code></pre>