feat:theme changing done

This commit is contained in:
jahongireshonqulov
2025-11-01 15:37:05 +05:00
parent 44878e79b3
commit b734fc9ce6
9 changed files with 142 additions and 45 deletions

View File

@@ -0,0 +1,37 @@
import 'package:food_delivery_client/food_delivery_client.dart';
part 'theme_event.dart';
part 'theme_state.dart';
part 'theme_bloc.freezed.dart';
@injectable
class ThemeBloc extends Bloc<ThemeEvent, ThemeState> {
final StorageService _storageService;
ThemeBloc(this._storageService) : super(const ThemeState()) {
on<_Started>(_onStarted);
on<_Changed>(_onChanged);
}
void _onStarted(_Started event, Emitter<ThemeState> emit) {
final isDark = _storageService.getBool(key: AppLocaleKeys.theme);
if (isDark) {
emit(state.copyWith(themeMode: ThemeMode.dark));
} else {
emit(state.copyWith(themeMode: ThemeMode.light));
}
}
void _onChanged(_Changed event, Emitter<ThemeState> emit) {
if (state.themeMode == ThemeMode.light) {
_storageService.setBool(key: AppLocaleKeys.theme, value: true);
emit(state.copyWith(themeMode: ThemeMode.dark));
} else {
_storageService.setBool(key: AppLocaleKeys.theme, value: false);
emit(state.copyWith(themeMode: ThemeMode.light));
}
}
}

View File

@@ -0,0 +1,8 @@
part of 'theme_bloc.dart';
@freezed
class ThemeEvent with _$ThemeEvent {
const factory ThemeEvent.started() = _Started;
const factory ThemeEvent.changed() = _Changed;
}

View File

@@ -0,0 +1,7 @@
part of 'theme_bloc.dart';
@freezed
abstract class ThemeState with _$ThemeState {
const factory ThemeState({@Default(ThemeMode.light) ThemeMode themeMode}) =
_ThemeState;
}

View File

@@ -1,21 +1,35 @@
import '../../../../../food_delivery_client.dart';
import '../../../common/presentation/blocs/theme_bloc/theme_bloc.dart';
class ProfilePage extends StatelessWidget {
const ProfilePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Home"),
GradientSwitch(value:false, onChanged: (value) {
print(value);
}),
],
),
return BlocBuilder<ThemeBloc, ThemeState>(
builder: (context, state) {
return Scaffold(
body: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Home"),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("Dark mode"),
GradientSwitch(
value: state.themeMode == ThemeMode.dark,
onChanged: (value) {
context.read<ThemeBloc>().add(ThemeEvent.changed());
},
),
],
).paddingSymmetric(horizontal: 20),
],
),
);
},
);
}
}