InjectedThemeImp<KEY> constructor

InjectedThemeImp<KEY>({
  1. required Map<KEY, ThemeData> lightThemes,
  2. required Map<KEY, ThemeData>? darkThemes,
  3. required ThemeMode themeModel,
  4. required String? persistKey,
  5. required StateInterceptor<KEY>? stateInterceptor,
  6. required SideEffects<KEY>? sideEffects,
  7. required DependsOn<KEY>? dependsOn,
  8. required int undoStackLength,
  9. required bool autoDisposeWhenNotUsed,
  10. required String? debugPrintWhenNotifiedPreMessage,
  11. required Object? toDebugString(
    1. KEY?
    )?,
})

Implementation

InjectedThemeImp({
  required this.lightThemes,
  required this.darkThemes,
  required ThemeMode themeModel,
  required String? persistKey,
  //
  required StateInterceptor<KEY>? stateInterceptor,
  required SideEffects<KEY>? sideEffects,
  //
  required DependsOn<KEY>? dependsOn,
  required int undoStackLength,
  //
  required bool autoDisposeWhenNotUsed,
  required String? debugPrintWhenNotifiedPreMessage,
  required Object? Function(KEY?)? toDebugString,
}) : _initialThemeMode = themeModel {
  final persist = persistKey == null
      ? null
      : PersistState(
          key: persistKey,
          fromJson: (json) {
            ///json is of the form key#|#1
            final s = json.split('#|#');
            assert(s.length <= 2);
            final KEY key = lightThemes.keys.firstWhere(
              (k) => s.first == '$k',
              orElse: () => lightThemes.keys.first,
            );
            //
            if (s.last == '0') {
              _themeMode = ThemeMode.light;
            } else if (s.last == '1') {
              _themeMode = ThemeMode.dark;
            } else {
              _themeMode = ThemeMode.system;
            }
            return key;
          },
          toJson: (key) {
            String th = '';
            if (_themeMode == ThemeMode.light) {
              th = '0';
            } else if (_themeMode == ThemeMode.dark) {
              th = '1';
            }

            ///json is of the form key#|#1
            return '$key#|#$th';
          },
          // debugPrintOperations: true,
        );
  injected = Injected<KEY>(
    creator: () => lightThemes.keys.first,
    initialState: lightThemes.keys.first,
    sideEffects: SideEffects<KEY>(
      initState: sideEffects?.initState,
      dispose: () {
        sideEffects?.dispose?.call();
        _resetDefaultState();
      },
      onAfterBuild: sideEffects?.onAfterBuild,
      onSetState: sideEffects?.onSetState != null
          ? (snap) {
              //For InjectedI18N and InjectedTheme schedule side effects
              //for the next frame.
              WidgetsBinding.instance.addPostFrameCallback(
                (_) => sideEffects!.onSetState!(snap),
              );
            }
          : null,
    ),
    stateInterceptor: stateInterceptor,
    persist: persist != null ? () => persist : null,
    undoStackLength: undoStackLength,
    dependsOn: dependsOn,
    autoDisposeWhenNotUsed: autoDisposeWhenNotUsed,
    debugPrintWhenNotifiedPreMessage: debugPrintWhenNotifiedPreMessage,
    toDebugString: toDebugString,
  ) as InjectedImp<KEY>;
  _resetDefaultState = () {
    _isDarkTheme = false;
    _themeMode = _initialThemeMode;
    isLinkedToTopStatelessWidget = false;
  };
  _resetDefaultState();
}