theme work

This commit is contained in:
2026-04-05 10:06:26 +02:00
parent 0347a354ef
commit 28b2abdff3
16 changed files with 223 additions and 137 deletions

View File

@@ -1,17 +1,30 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flux/data/enums.dart';
import 'package:get_it/get_it.dart';
import 'package:shared_preferences/shared_preferences.dart';
part 'theme_events.dart';
part 'theme_state.dart';
class ThemeBloc extends Bloc<ThemeEvent, ThemeState> {
final AppTheme initialAppTheme;
ThemeBloc({required this.initialAppTheme})
: super(
ThemeState(status: ThemeStatus.success, appTheme: initialAppTheme),
) {
static const String _savedThemeKey = "themeModeSetting";
final SharedPreferences _prefs = GetIt.I.get<SharedPreferences>();
ThemeBloc() : super(ThemeState(themeMode: ThemeMode.system)) {
on<LoadThemeEvent>((event, emit) {
String savedTheme = _prefs.getString(_savedThemeKey) ?? 'system';
emit(
state.copyWith(
themeMode: AppThemeMode.values
.firstWhere((test) => test.value == savedTheme)
.themeMode,
),
);
});
on<ChangeThemeEvent>((event, emit) async {
emit(state.copyWith(appTheme: event.appTheme));
await _prefs.setString(_savedThemeKey, event.appThemeMode.value);
emit(state.copyWith(themeMode: event.appThemeMode.themeMode));
});
}
}