refactor
This commit is contained in:
27
lib/core/theme/bloc/theme_bloc.dart
Normal file
27
lib/core/theme/bloc/theme_bloc.dart
Normal file
@@ -0,0 +1,27 @@
|
||||
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 SharedPreferences _prefs = GetIt.I.get<SharedPreferences>();
|
||||
ThemeBloc() : super(ThemeState(currentTheme: AppThemeMode.system)) {
|
||||
on<LoadThemeEvent>((event, emit) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
currentTheme: AppThemeMode.fromValue(
|
||||
_prefs.getString(PrefKeys.theme.value),
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
on<ChangeThemeEvent>((event, emit) async {
|
||||
await _prefs.setString(PrefKeys.theme.value, event.appThemeMode.value);
|
||||
emit(state.copyWith(currentTheme: event.appThemeMode));
|
||||
});
|
||||
}
|
||||
}
|
||||
19
lib/core/theme/bloc/theme_events.dart
Normal file
19
lib/core/theme/bloc/theme_events.dart
Normal file
@@ -0,0 +1,19 @@
|
||||
part of 'theme_bloc.dart';
|
||||
|
||||
abstract class ThemeEvent extends Equatable {
|
||||
const ThemeEvent();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
final class LoadThemeEvent extends ThemeEvent {}
|
||||
|
||||
final class ChangeThemeEvent extends ThemeEvent {
|
||||
final AppThemeMode appThemeMode;
|
||||
|
||||
const ChangeThemeEvent(this.appThemeMode);
|
||||
|
||||
@override
|
||||
List<Object> get props => [appThemeMode];
|
||||
}
|
||||
14
lib/core/theme/bloc/theme_state.dart
Normal file
14
lib/core/theme/bloc/theme_state.dart
Normal file
@@ -0,0 +1,14 @@
|
||||
part of 'theme_bloc.dart';
|
||||
|
||||
class ThemeState extends Equatable {
|
||||
const ThemeState({required this.currentTheme});
|
||||
|
||||
final AppThemeMode currentTheme;
|
||||
|
||||
@override
|
||||
List<Object?> get props => [currentTheme];
|
||||
|
||||
ThemeState copyWith({AppThemeMode? currentTheme}) {
|
||||
return ThemeState(currentTheme: currentTheme ?? this.currentTheme);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user