Skip to content

Commit

Permalink
ref: Change ChangeNotifier to Cubit for app theme
Browse files Browse the repository at this point in the history
  • Loading branch information
0niel committed Jan 10, 2024
1 parent 41c81ba commit 14dba8b
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 47 deletions.
72 changes: 46 additions & 26 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ import 'package:shared_preferences/shared_preferences.dart';
import 'package:university_app_server_api/client.dart';
import 'package:url_strategy/url_strategy.dart';
import 'common/constants.dart';
import 'presentation/app_notifier.dart';
import 'presentation/app_theme_cubit.dart';
import 'service_locator.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
import 'package:provider/provider.dart';
import 'package:sentry_logging/sentry_logging.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:schedule_repository/schedule_repository.dart'
Expand Down Expand Up @@ -131,10 +130,7 @@ Future<void> main() async {
/// The AssetBundle instrumentation provides insight into how long
/// app takes to load its assets, such as files
bundle: SentryAssetBundle(),
child: ChangeNotifierProvider<AppNotifier>(
create: (context) => getIt<AppNotifier>(),
child: const App(),
),
child: const App(),
),
),
),
Expand All @@ -149,16 +145,14 @@ class App extends StatelessWidget {

@override
Widget build(BuildContext context) {
final router = getIt<GoRouter>();

// blocking the orientation of the application to
// vertical only
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitDown,
DeviceOrientation.portraitUp,
]);

final apiClient = ApiClient();
final apiClient = ApiClient.localhost();

return MultiRepositoryProvider(
providers: [
Expand All @@ -174,6 +168,9 @@ class App extends StatelessWidget {
],
child: MultiBlocProvider(
providers: [
BlocProvider<AppThemeCubit>(
create: (context) => getIt<AppThemeCubit>(),
),
BlocProvider<ScheduleBloc>(
create: (context) => ScheduleBloc(
scheduleRepository:
Expand Down Expand Up @@ -211,29 +208,52 @@ class App extends StatelessWidget {
create: (_) => getIt<NotificationPreferencesBloc>(),
),
],
child: Consumer<AppNotifier>(
builder: (BuildContext context, AppNotifier value, Widget? child) {
return MaterialApp.router(
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [
Locale('en'),
Locale('ru'),
],
locale: const Locale('ru'),
debugShowCheckedModeBanner: false,
title: 'Приложение РТУ МИРЭА',
routerConfig: router,
child: BlocBuilder<AppThemeCubit, AppThemeState>(
builder: (context, state) {
return _MaterialApp(
themeMode: AppTheme.themeMode,
theme: AppTheme.theme,
darkTheme: AppTheme.darkTheme,
);
},
),
),
);
}
}

class _MaterialApp extends StatefulWidget {
const _MaterialApp({
Key? key,
required this.themeMode,
required this.theme,
}) : super(key: key);

final ThemeMode themeMode;
final ThemeData theme;

@override
State<_MaterialApp> createState() => _MaterialAppState();
}

class _MaterialAppState extends State<_MaterialApp> {
@override
Widget build(BuildContext context) {
return MaterialApp.router(
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [
Locale('en'),
Locale('ru'),
],
locale: const Locale('ru'),
debugShowCheckedModeBanner: false,
title: 'Приложение РТУ МИРЭА',
routerConfig: getIt<GoRouter>(),
themeMode: widget.themeMode,
theme: widget.theme,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ import 'package:rtu_mirea_app/domain/usecases/get_app_settings.dart';
import 'package:rtu_mirea_app/domain/usecases/set_app_settings.dart';
import 'package:rtu_mirea_app/presentation/theme.dart';

class AppNotifier extends ChangeNotifier {
import 'package:flutter_bloc/flutter_bloc.dart';


class AppThemeCubit extends Cubit<AppThemeState> {
final GetAppSettings getAppSettings;
final SetAppSettings setAppSettings;

AppNotifier({required this.getAppSettings, required this.setAppSettings}) {
AppThemeCubit({required this.getAppSettings, required this.setAppSettings})
: super(AppInitialState()) {
init();
}

init() async {
void init() async {
final settings = await getAppSettings();

AppThemeType themeType = AppThemeType.values.firstWhere(
Expand All @@ -22,15 +26,15 @@ class AppNotifier extends ChangeNotifier {

_changeTheme(themeType);

notifyListeners();
emit(AppLoadedState(themeType));
}

updateTheme(AppThemeType themeType) {
void updateTheme(AppThemeType themeType) async {
_changeTheme(themeType);

notifyListeners();
emit(AppLoadedState(themeType));

updateInStorage(themeType);
await updateInStorage(themeType);
}

Future<void> updateInStorage(AppThemeType themeType) async {
Expand All @@ -49,7 +53,18 @@ class AppNotifier extends ChangeNotifier {

void _changeTheme(AppThemeType themeType) {
AppTheme.changeThemeType(themeType);

// Reset custom theme here if needed
}
}

abstract class AppThemeState {
ThemeMode get themeMode => AppTheme.themeMode;
}

class AppInitialState extends AppThemeState {}

class AppLoadedState extends AppThemeState {
final AppThemeType themeType;

AppLoadedState(this.themeType);
}
9 changes: 6 additions & 3 deletions lib/presentation/pages/profile/profile_settings_page.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';
import 'package:rtu_mirea_app/presentation/app_notifier.dart';
import 'package:rtu_mirea_app/presentation/app_theme_cubit.dart';
import 'package:rtu_mirea_app/presentation/theme.dart';
import 'package:rtu_mirea_app/presentation/typography.dart';

Expand Down Expand Up @@ -42,10 +43,11 @@ class ProfileSettingsPage extends StatelessWidget {
: null,
onTap: () {
context
.read<AppNotifier>()
.read<AppThemeCubit>()
.updateTheme(AppThemeType.light);

context.pop();
SystemNavigator.pop();
},
),
const SizedBox(height: 8),
Expand All @@ -56,10 +58,11 @@ class ProfileSettingsPage extends StatelessWidget {
: null,
onTap: () {
context
.read<AppNotifier>()
.read<AppThemeCubit>()
.updateTheme(AppThemeType.dark);

context.pop();
SystemNavigator.pop();
},
),
],
Expand Down
10 changes: 2 additions & 8 deletions lib/presentation/pages/scaffold_with_nav_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import 'dart:io' show Platform;

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';
import 'package:rtu_mirea_app/presentation/app_notifier.dart';
import 'package:rtu_mirea_app/presentation/theme.dart';
import 'package:rtu_mirea_app/presentation/typography.dart';
import 'package:unicons/unicons.dart';
Expand All @@ -29,19 +27,15 @@ class ScaffoldWithNavBar extends StatelessWidget {
children: [
_buildSidebar(context),
Expanded(
child: Consumer<AppNotifier>(
builder: (context, value, child) => navigationShell,
),
child: navigationShell,
),
],
),
);
} else {
return Scaffold(
backgroundColor: AppTheme.colors.background03,
body: Consumer<AppNotifier>(
builder: (_, value, child) => navigationShell,
),
body: navigationShell,
bottomNavigationBar:
navigationShell.route.toString().contains('home')
? null
Expand Down
2 changes: 2 additions & 0 deletions lib/presentation/theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class AppTheme {
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
insetPadding: const EdgeInsets.all(16),
),
colorScheme: ColorScheme(
background: darkThemeColors.background01,
Expand Down Expand Up @@ -171,6 +172,7 @@ class AppTheme {
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
insetPadding: const EdgeInsets.all(16),
),
dividerTheme: DividerThemeData(
color: lightThemeColors.deactiveDarker.withOpacity(0.25),
Expand Down
4 changes: 2 additions & 2 deletions lib/service_locator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import 'package:rtu_mirea_app/domain/usecases/send_nfc_not_exist_feedback.dart';
import 'package:rtu_mirea_app/domain/usecases/set_active_group.dart';
import 'package:rtu_mirea_app/domain/usecases/set_app_settings.dart';
import 'package:rtu_mirea_app/domain/usecases/set_schedule_settings.dart';
import 'package:rtu_mirea_app/presentation/app_notifier.dart';
import 'package:rtu_mirea_app/presentation/app_theme_cubit.dart';
import 'package:rtu_mirea_app/presentation/bloc/announces_bloc/announces_bloc.dart';
import 'package:rtu_mirea_app/presentation/bloc/app_cubit/app_cubit.dart';
import 'package:rtu_mirea_app/presentation/bloc/attendance_bloc/attendance_bloc.dart';
Expand All @@ -70,7 +70,7 @@ final getIt = GetIt.instance;
Future<void> setup() async {
// Global app notifier
getIt.registerFactory(
() => AppNotifier(
() => AppThemeCubit(
getAppSettings: getIt(),
setAppSettings: getIt(),
),
Expand Down

0 comments on commit 14dba8b

Please sign in to comment.