refactor(theme&ui): improve theme switching and navigation styling
This commit is contained in:
33
lib/product/services/theme_services.dart
Normal file
33
lib/product/services/theme_services.dart
Normal file
@@ -0,0 +1,33 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../cache/local_manager.dart';
|
||||
import '../constant/enums/app_theme_enums.dart';
|
||||
import '../constant/enums/local_keys_enums.dart';
|
||||
import '../theme/app_theme_dark.dart';
|
||||
import '../theme/app_theme_light.dart';
|
||||
|
||||
class ThemeServices{
|
||||
|
||||
ThemeData _currentTheme(String theme) {
|
||||
if (theme == AppThemes.LIGHT.name) {
|
||||
return AppThemeLight.instance.theme;
|
||||
} else if (theme == AppThemes.DARK.name) {
|
||||
return AppThemeDark.instance.theme;
|
||||
} else {
|
||||
return AppThemeLight.instance.theme;
|
||||
}
|
||||
}
|
||||
|
||||
Future<ThemeData> getTheme() async {
|
||||
await LocaleManager.prefrencesInit();
|
||||
String theme = LocaleManager.instance.getStringValue(PreferencesKeys.THEME);
|
||||
return _currentTheme(theme);
|
||||
}
|
||||
|
||||
Future<ThemeData> changeTheme(String theme) async {
|
||||
await LocaleManager.prefrencesInit();
|
||||
LocaleManager.instance
|
||||
.setStringValue(PreferencesKeys.THEME, theme);
|
||||
return _currentTheme(theme);
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:provider/single_child_widget.dart';
|
||||
|
||||
import '../theme_notifier.dart';
|
||||
|
||||
class ApplicationProvider {
|
||||
static ApplicationProvider? _instance;
|
||||
static ApplicationProvider get instance {
|
||||
_instance ??= ApplicationProvider._init();
|
||||
return _instance!;
|
||||
}
|
||||
|
||||
ApplicationProvider._init();
|
||||
|
||||
List<SingleChildWidget> singleItems = [];
|
||||
List<SingleChildWidget> dependItems = [
|
||||
ChangeNotifierProvider(
|
||||
create: (context) => ThemeNotifier(),
|
||||
),
|
||||
// Provider.value(value: NavigationService.instance)
|
||||
];
|
||||
List<SingleChildWidget> uiChangesItems = [];
|
||||
}
|
||||
@@ -1,115 +0,0 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'app_theme_dark.dart';
|
||||
import 'app_theme_light.dart';
|
||||
|
||||
import '../constant/enums/app_theme_enums.dart';
|
||||
|
||||
class ThemeNotifier extends ChangeNotifier {
|
||||
// ThemeData _currentTheme = AppThemeLight.instance.theme;
|
||||
// // ThemeData get currentTheme => LocaleManager.instance.getStringValue(PreferencesKeys.THEME) ==
|
||||
// // AppThemes.LIGHT.name
|
||||
// // ? AppThemeLight.instance.theme
|
||||
// // : AppThemeDark.instance.theme;
|
||||
// ThemeData get currentTheme {
|
||||
// log("ThemeKey: ${LocaleManager.instance.getStringValue(PreferencesKeys.THEME)}");
|
||||
// if (LocaleManager.instance.getStringValue(PreferencesKeys.THEME) ==
|
||||
// AppThemes.LIGHT.name) {
|
||||
// log("light");
|
||||
// } else {
|
||||
// log("dark");
|
||||
// }
|
||||
// return LocaleManager.instance.getStringValue(PreferencesKeys.THEME) ==
|
||||
// AppThemes.LIGHT.name
|
||||
// ? AppThemeLight.instance.theme
|
||||
// : AppThemeDark.instance.theme;
|
||||
// }
|
||||
|
||||
// ThemeData _currentTheme = AppThemeLight.instance.theme; // Mặc định là light
|
||||
|
||||
// ThemeNotifier() {
|
||||
// loadThemeFromPreferences();
|
||||
// }
|
||||
|
||||
// Future<void> loadThemeFromPreferences() async {
|
||||
// String themeKey =
|
||||
// LocaleManager.instance.getStringValue(PreferencesKeys.THEME);
|
||||
// log("ThemeNotifierKey:$themeKey ");
|
||||
// if (themeKey == AppThemes.LIGHT.name) {
|
||||
// _currentTheme = AppThemeLight.instance.theme;
|
||||
// } else {
|
||||
// _currentTheme = AppThemeDark.instance.theme;
|
||||
// }
|
||||
// notifyListeners(); // Thông báo cho các widget lắng nghe
|
||||
// }
|
||||
|
||||
// ThemeData get currentTheme => _currentTheme;
|
||||
|
||||
// AppThemes _currenThemeEnum = AppThemes.LIGHT;
|
||||
// AppThemes get currenThemeEnum =>
|
||||
// LocaleManager.instance.getStringValue(PreferencesKeys.THEME) ==
|
||||
// AppThemes.LIGHT.name
|
||||
// ? AppThemes.LIGHT
|
||||
// : AppThemes.DARK;
|
||||
// // AppThemes get currenThemeEnum => _currenThemeEnum;
|
||||
|
||||
// void changeValue(AppThemes theme) {
|
||||
// if (theme == AppThemes.LIGHT) {
|
||||
// _currentTheme = ThemeData.dark();
|
||||
// } else {
|
||||
// _currentTheme = ThemeData.light();
|
||||
// }
|
||||
// notifyListeners();
|
||||
// }
|
||||
|
||||
// void changeTheme() {
|
||||
// if (_currenThemeEnum == AppThemes.LIGHT) {
|
||||
// _currentTheme = AppThemeDark.instance.theme;
|
||||
// _currenThemeEnum = AppThemes.DARK;
|
||||
// LocaleManager.instance
|
||||
// .setString(PreferencesKeys.THEME, AppThemes.DARK.name);
|
||||
// } else {
|
||||
// _currentTheme = AppThemeLight.instance.theme;
|
||||
// _currenThemeEnum = AppThemes.LIGHT;
|
||||
// LocaleManager.instance
|
||||
// .setString(PreferencesKeys.THEME, AppThemes.LIGHT.name);
|
||||
// }
|
||||
// notifyListeners();
|
||||
// }
|
||||
|
||||
ThemeData _currentTheme = AppThemeLight.instance.theme;
|
||||
ThemeData get currentTheme => _currentTheme;
|
||||
|
||||
AppThemes _currenThemeEnum = AppThemes.LIGHT;
|
||||
AppThemes get currenThemeEnum => _currenThemeEnum;
|
||||
|
||||
Future<void> loadThemeFromPreferences() async {
|
||||
// String themeKey =
|
||||
// LocaleManager.instance.getStringValue(PreferencesKeys.THEME);
|
||||
// if (themeKey == AppThemes.LIGHT.name) {
|
||||
// _currentTheme = AppThemeLight.instance.theme;
|
||||
// } else {
|
||||
// _currentTheme = AppThemeDark.instance.theme;
|
||||
// }
|
||||
// notifyListeners();
|
||||
}
|
||||
|
||||
void changeValue(AppThemes theme) {
|
||||
if (theme == AppThemes.LIGHT) {
|
||||
_currentTheme = AppThemeLight.instance.theme;
|
||||
} else {
|
||||
_currentTheme = AppThemeDark.instance.theme;
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void changeTheme() {
|
||||
if (_currenThemeEnum == AppThemes.LIGHT) {
|
||||
_currentTheme = AppThemeDark.instance.theme;
|
||||
_currenThemeEnum = AppThemes.DARK;
|
||||
} else {
|
||||
_currentTheme = AppThemeLight.instance.theme;
|
||||
_currenThemeEnum = AppThemes.LIGHT;
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user