Skip to content

Commit 6e1ff65

Browse files
committed
refactor: integrate theme mode into AppBloc
- Removed ThemeCubit - Added AppThemeChanged event - Managed theme in AppBloc
1 parent cfbe479 commit 6e1ff65

File tree

8 files changed

+45
-55
lines changed

8 files changed

+45
-55
lines changed

lib/app/bloc/app_bloc.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'package:bloc/bloc.dart';
22
import 'package:equatable/equatable.dart';
3-
import 'package:meta/meta.dart';
3+
import 'package:flutter/material.dart';
44

55
part 'app_event.dart';
66
part 'app_state.dart';
@@ -10,5 +10,14 @@ class AppBloc extends Bloc<AppEvent, AppState> {
1010
on<AppNavigationIndexChanged>((event, emit) {
1111
emit(state.copyWith(selectedBottomNavigationIndex: event.index));
1212
});
13+
on<AppThemeChanged>((event, emit) {
14+
emit(
15+
state.copyWith(
16+
themeMode: state.themeMode == ThemeMode.light
17+
? ThemeMode.dark
18+
: ThemeMode.light,
19+
),
20+
);
21+
});
1322
}
1423
}

lib/app/bloc/app_event.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ final class AppNavigationIndexChanged extends AppEvent {
88

99
final int index;
1010
}
11+
12+
final class AppThemeChanged extends AppEvent {}

lib/app/bloc/app_state.dart

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
part of 'app_bloc.dart';
22

33
class AppState extends Equatable {
4-
const AppState({this.selectedBottomNavigationIndex = 0});
4+
const AppState({
5+
this.selectedBottomNavigationIndex = 0,
6+
this.themeMode = ThemeMode.system,
7+
});
58
final int selectedBottomNavigationIndex;
9+
final ThemeMode themeMode;
610

7-
AppState copyWith({int? selectedBottomNavigationIndex}) {
11+
AppState copyWith({
12+
int? selectedBottomNavigationIndex,
13+
ThemeMode? themeMode,
14+
}) {
815
return AppState(
916
selectedBottomNavigationIndex:
1017
selectedBottomNavigationIndex ?? this.selectedBottomNavigationIndex,
18+
themeMode: themeMode ?? this.themeMode,
1119
);
1220
}
1321

1422
@override
15-
List<Object?> get props => [selectedBottomNavigationIndex];
23+
List<Object?> get props => [selectedBottomNavigationIndex, themeMode];
1624
}

lib/app/view/app.dart

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,37 @@
1+
import 'package:flex_color_scheme/flex_color_scheme.dart';
12
import 'package:flutter/material.dart';
23
import 'package:flutter_bloc/flutter_bloc.dart';
4+
import 'package:google_fonts/google_fonts.dart';
5+
import 'package:ht_main/app/bloc/app_bloc.dart';
36
import 'package:ht_main/l10n/l10n.dart';
47
import 'package:ht_main/router/router.dart';
5-
import 'package:ht_main/theme/cubit/theme_cubit.dart';
8+
9+
ThemeData lightTheme() {
10+
return FlexThemeData.light(
11+
scheme: FlexScheme.material,
12+
fontFamily: GoogleFonts.notoSans().fontFamily,
13+
);
14+
}
15+
16+
ThemeData darkTheme() {
17+
return FlexThemeData.dark(
18+
scheme: FlexScheme.material,
19+
fontFamily: GoogleFonts.notoSans().fontFamily,
20+
);
21+
}
622

723
class App extends StatelessWidget {
824
const App({super.key});
925

1026
@override
1127
Widget build(BuildContext context) {
1228
return BlocProvider(
13-
create: (_) => ThemeCubit(),
14-
child: BlocBuilder<ThemeCubit, ThemeState>(
29+
create: (_) => AppBloc(),
30+
child: BlocBuilder<AppBloc, AppState>(
1531
builder: (context, state) {
1632
return MaterialApp.router(
17-
theme: state.themeData,
33+
theme:
34+
state.themeMode == ThemeMode.light ? lightTheme() : darkTheme(),
1835
routerConfig: appRouter,
1936
localizationsDelegates: AppLocalizations.localizationsDelegates,
2037
supportedLocales: AppLocalizations.supportedLocales,

lib/theme/cubit/theme_cubit.dart

Lines changed: 0 additions & 13 deletions
This file was deleted.

lib/theme/cubit/theme_state.dart

Lines changed: 0 additions & 14 deletions
This file was deleted.

lib/theme/theme.dart

Lines changed: 0 additions & 19 deletions
This file was deleted.

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: ht_main
22
description: main headlines toolkit mobile app.
3-
version: 0.3.3
3+
version: 0.3.5
44
publish_to: none
55
repository: https://github.com/Headlines-Toolkit/ht-main
66
environment:

0 commit comments

Comments
 (0)