feat:theme changing done
This commit is contained in:
@@ -3,6 +3,8 @@ abstract class AppLocaleKeys {
|
||||
static const String language = 'language';
|
||||
static const String browseSearchHistory = 'browse-search-history';
|
||||
static const String token = 'token';
|
||||
static const String theme = 'theme';
|
||||
|
||||
|
||||
static const String appName = "Felix Eats";
|
||||
|
||||
|
||||
@@ -40,6 +40,8 @@ import '../../feature/auth/presentation/blocs/verify_phone_bloc/verify_phone_blo
|
||||
as _i224;
|
||||
import '../../feature/common/presentation/blocs/language_bloc/language_bloc.dart'
|
||||
as _i942;
|
||||
import '../../feature/common/presentation/blocs/theme_bloc/theme_bloc.dart'
|
||||
as _i465;
|
||||
import '../../feature/main/presentation/blocs/main_bloc/main_bloc.dart'
|
||||
as _i580;
|
||||
import '../../feature/onboarding/presentation/blocs/splash_bloc/splash_bloc.dart'
|
||||
@@ -68,6 +70,9 @@ extension GetItInjectableX on _i174.GetIt {
|
||||
gh.factory<_i28.SplashBloc>(
|
||||
() => _i28.SplashBloc(gh<_i321.StorageService>()),
|
||||
);
|
||||
gh.factory<_i465.ThemeBloc>(
|
||||
() => _i465.ThemeBloc(gh<_i321.StorageService>()),
|
||||
);
|
||||
gh.lazySingleton<_i361.Dio>(() => dioModule.dio(gh<_i667.DioClient>()));
|
||||
gh.singleton<_i354.RequestHandlerService>(
|
||||
() => _i354.RequestHandlerService(gh<_i361.Dio>()),
|
||||
|
||||
@@ -8,6 +8,10 @@ class StorageService {
|
||||
_sharedPreference = await SharedPreferences.getInstance();
|
||||
}
|
||||
|
||||
void setBool({required String key, required bool value}) {
|
||||
_sharedPreference.setBool(key, value);
|
||||
}
|
||||
|
||||
void setString({required String key, required String value}) {
|
||||
_sharedPreference.setString(key, value);
|
||||
}
|
||||
@@ -25,6 +29,10 @@ class StorageService {
|
||||
String? getString({required String key}) {
|
||||
return _sharedPreference.getString(key);
|
||||
}
|
||||
|
||||
bool getBool({required String key}) {
|
||||
return _sharedPreference.getBool(key) ?? false;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
part of 'theme_bloc.dart';
|
||||
|
||||
@freezed
|
||||
class ThemeEvent with _$ThemeEvent {
|
||||
const factory ThemeEvent.started() = _Started;
|
||||
const factory ThemeEvent.changed() = _Changed;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
part of 'theme_bloc.dart';
|
||||
|
||||
@freezed
|
||||
abstract class ThemeState with _$ThemeState {
|
||||
const factory ThemeState({@Default(ThemeMode.light) ThemeMode themeMode}) =
|
||||
_ThemeState;
|
||||
}
|
||||
@@ -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),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import 'package:food_delivery_client/feature/common/presentation/blocs/theme_bloc/theme_bloc.dart';
|
||||
import 'package:toastification/toastification.dart';
|
||||
|
||||
import 'food_delivery_client.dart';
|
||||
|
||||
void main() {
|
||||
@@ -13,7 +13,13 @@ void main() {
|
||||
Bloc.observer = AppBlocObserver();
|
||||
await configureDependencies();
|
||||
runApp(
|
||||
BlocProvider(create: (context) => sl<LanguageBloc>(), child: MyApp()),
|
||||
MultiBlocProvider(
|
||||
providers: [
|
||||
BlocProvider(create: (context) => sl<LanguageBloc>()),
|
||||
BlocProvider(create: (context) => sl<ThemeBloc>()),
|
||||
],
|
||||
child: MyApp(),
|
||||
),
|
||||
);
|
||||
},
|
||||
(error, stack) {
|
||||
@@ -32,46 +38,54 @@ class MyApp extends StatefulWidget {
|
||||
class _MyAppState extends State<MyApp> {
|
||||
@override
|
||||
void initState() {
|
||||
SystemChrome.setPreferredOrientations([
|
||||
DeviceOrientation.portraitUp,
|
||||
DeviceOrientation.portraitDown,
|
||||
]);
|
||||
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
|
||||
SystemChrome.setPreferredOrientations([
|
||||
DeviceOrientation.portraitUp,
|
||||
DeviceOrientation.portraitDown,
|
||||
]);
|
||||
context.read<LanguageBloc>().add(LanguageEvent.started());
|
||||
context.read<ThemeBloc>().add(ThemeEvent.started());
|
||||
});
|
||||
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<LanguageBloc, LanguageState>(
|
||||
bloc: context.read<LanguageBloc>()..add(LanguageEvent.started()),
|
||||
builder: (context, state) {
|
||||
return ToastificationWrapper(
|
||||
child: MaterialApp.router(
|
||||
title: AppLocaleKeys.appName,
|
||||
debugShowCheckedModeBanner: false,
|
||||
theme: AppTheme.lightTheme,
|
||||
darkTheme: AppTheme.darkTheme,
|
||||
themeMode: ThemeMode.light,
|
||||
routerConfig: sl<AppRoutes>().router,
|
||||
locale: state.currentLocale,
|
||||
supportedLocales: L10n.locales,
|
||||
localizationsDelegates: [
|
||||
AppLocalizations.delegate,
|
||||
GlobalMaterialLocalizations.delegate,
|
||||
GlobalWidgetsLocalizations.delegate,
|
||||
GlobalCupertinoLocalizations.delegate,
|
||||
],
|
||||
builder: (context, child) => GestureDetector(
|
||||
onTap: () {
|
||||
FocusManager.instance.primaryFocus?.unfocus();
|
||||
},
|
||||
child: MediaQuery(
|
||||
data: MediaQuery.of(
|
||||
context,
|
||||
).copyWith(textScaler: const TextScaler.linear(1)),
|
||||
child: child!,
|
||||
return BlocBuilder<ThemeBloc, ThemeState>(
|
||||
builder: (context, themeState) {
|
||||
return ToastificationWrapper(
|
||||
child: MaterialApp.router(
|
||||
title: AppLocaleKeys.appName,
|
||||
debugShowCheckedModeBanner: false,
|
||||
theme: AppTheme.lightTheme,
|
||||
darkTheme: AppTheme.darkTheme,
|
||||
themeMode: themeState.themeMode,
|
||||
routerConfig: sl<AppRoutes>().router,
|
||||
locale: state.currentLocale,
|
||||
supportedLocales: L10n.locales,
|
||||
localizationsDelegates: [
|
||||
AppLocalizations.delegate,
|
||||
GlobalMaterialLocalizations.delegate,
|
||||
GlobalWidgetsLocalizations.delegate,
|
||||
GlobalCupertinoLocalizations.delegate,
|
||||
],
|
||||
builder: (context, child) => GestureDetector(
|
||||
onTap: () {
|
||||
FocusManager.instance.primaryFocus?.unfocus();
|
||||
},
|
||||
child: MediaQuery(
|
||||
data: MediaQuery.of(
|
||||
context,
|
||||
).copyWith(textScaler: const TextScaler.linear(1)),
|
||||
child: child!,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user