From 0f1f94976d60e5e88027a8a110c532a2a7653666 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Chiotti?= Date: Mon, 20 May 2024 14:33:15 +0200 Subject: [PATCH 1/2] style: refactor theme manager to simplify creating themes --- lib/app.dart | 16 ++---- lib/utils/theme_manager.dart | 96 ++++++++++++++++++------------------ 2 files changed, 51 insertions(+), 61 deletions(-) diff --git a/lib/app.dart b/lib/app.dart index cc9e1902..076e1aa3 100644 --- a/lib/app.dart +++ b/lib/app.dart @@ -43,7 +43,7 @@ class _AppState extends ConsumerState with AfterLayoutMixin { @override Widget build(BuildContext context) { return DynamicColorBuilder( - builder: (lightDynamic, darkDynamic) { + builder: (lightDynamicColorScheme, darkDynamicColorScheme) { return ValueListenableBuilder( valueListenable: dynamicThemingNotifier, builder: (_, __, ___) { @@ -53,20 +53,12 @@ class _AppState extends ConsumerState with AfterLayoutMixin { return ValueListenableBuilder( valueListenable: themeModeNotifier, builder: (_, themeMode, ___) { - final useDynamicTheming = ThemeManager().useDynamicTheming; - return MaterialApp.router( title: 'Material Notes', - theme: useDynamicTheming - ? ThemeManager().getLightDynamicTheme(lightDynamic) - : ThemeManager().getLightCustomTheme, - darkTheme: useDynamicTheming - ? ThemeManager().getDarkDynamicTheme(darkDynamic) - : ThemeManager().getDarkCustomTheme, + theme: ThemeManager().getLightTheme(lightDynamicColorScheme), + darkTheme: ThemeManager().getDarkTheme(darkDynamicColorScheme), themeMode: themeMode, - localizationsDelegates: const [ - ...AppLocalizations.localizationsDelegates, - ], + localizationsDelegates: AppLocalizations.localizationsDelegates, supportedLocales: AppLocalizations.supportedLocales, locale: LocaleManager().locale, routerConfig: router, diff --git a/lib/utils/theme_manager.dart b/lib/utils/theme_manager.dart index 751bcfb0..7b17e420 100644 --- a/lib/utils/theme_manager.dart +++ b/lib/utils/theme_manager.dart @@ -4,8 +4,6 @@ import 'package:localmaterialnotes/utils/constants/constants.dart'; import 'package:localmaterialnotes/utils/preferences/preference_key.dart'; import 'package:localmaterialnotes/utils/preferences/preferences_manager.dart'; -const _customPrimaryColor = Color(0xFF2278e9); - class ThemeManager { static final ThemeManager _singleton = ThemeManager._internal(); @@ -15,6 +13,8 @@ class ThemeManager { ThemeManager._internal(); + final _customPrimaryColor = const Color(0xFF2278e9); + late final bool isDynamicThemingAvailable; Future init() async { @@ -22,20 +22,6 @@ class ThemeManager { await DynamicColorPlugin.getCorePalette() != null || await DynamicColorPlugin.getAccentColor() != null; } - ColorScheme get _customLightColorScheme { - return ColorScheme.fromSeed( - seedColor: _customPrimaryColor, - ); - } - - ColorScheme get _customDarkColorScheme { - return ColorScheme.fromSeed( - brightness: Brightness.dark, - seedColor: _customPrimaryColor, - surface: useBlackTheming ? Colors.black : null, - ); - } - bool get useDynamicTheming { return PreferenceKey.dynamicTheming.getPreferenceOrDefault(); } @@ -78,6 +64,51 @@ class ThemeManager { return localizations.settings_theme_system; } + ThemeData getLightTheme(ColorScheme? lightDynamicColorScheme) { + final colorScheme = useDynamicTheming && lightDynamicColorScheme != null + ? lightDynamicColorScheme + : ColorScheme.fromSeed( + seedColor: _customPrimaryColor, + ); + + return ThemeData( + useMaterial3: true, + colorScheme: colorScheme, + ); + } + + ThemeData getDarkTheme(ColorScheme? darkDynamicColorScheme) { + final ColorScheme colorScheme; + + if (useDynamicTheming && darkDynamicColorScheme != null) { + colorScheme = useBlackTheming + ? darkDynamicColorScheme.copyWith( + // TODO: remove when dynamic_color is updated + // (cf. https://github.com/material-foundation/flutter-packages/issues/574 + // and https://github.com/material-foundation/flutter-packages/issues/582) + background: useBlackTheming ? Colors.black : null, // ignore: deprecated_member_use + surface: useBlackTheming ? Colors.black : null, + ) + : darkDynamicColorScheme; + } else { + colorScheme = useBlackTheming + ? ColorScheme.fromSeed( + brightness: Brightness.dark, + seedColor: _customPrimaryColor, + surface: Colors.black, + ) + : ColorScheme.fromSeed( + brightness: Brightness.dark, + seedColor: _customPrimaryColor, + ); + } + + return ThemeData( + useMaterial3: true, + colorScheme: colorScheme, + ); + } + void setThemeMode(ThemeMode? themeMode) { if (themeMode == null) { return; @@ -96,37 +127,4 @@ class ThemeManager { themeModeNotifier.value = themeMode; } - - ThemeData getLightDynamicTheme([ColorScheme? lightDynamicColorScheme]) { - return ThemeData( - useMaterial3: true, - colorScheme: lightDynamicColorScheme != null ? lightDynamicColorScheme.harmonized() : _customLightColorScheme, - ); - } - - ThemeData getDarkDynamicTheme([ColorScheme? darkDynamicColorScheme]) { - return ThemeData( - useMaterial3: true, - colorScheme: darkDynamicColorScheme?.copyWith( - // TODO: remove when dynamic_color is updated (cf. https://github.com/material-foundation/flutter-packages/issues/574 and https://github.com/material-foundation/flutter-packages/issues/582) - background: useBlackTheming ? Colors.black : null, // ignore: deprecated_member_use - surface: useBlackTheming ? Colors.black : null, - ) ?? - _customDarkColorScheme, - ); - } - - ThemeData get getLightCustomTheme { - return ThemeData( - useMaterial3: true, - colorScheme: _customLightColorScheme, - ); - } - - ThemeData get getDarkCustomTheme { - return ThemeData( - useMaterial3: true, - colorScheme: _customDarkColorScheme, - ); - } } From b3cbd57453df31829000b612b9157bd1bcdc1c4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Chiotti?= Date: Mon, 20 May 2024 14:45:48 +0200 Subject: [PATCH 2/2] style: remove some theme values that were deprecated but ignored --- lib/common/widgets/note_tile.dart | 4 +--- lib/pages/editor/editor_page.dart | 3 +-- lib/utils/theme_manager.dart | 8 +++----- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/lib/common/widgets/note_tile.dart b/lib/common/widgets/note_tile.dart index 4be4c5f9..701bfa29 100644 --- a/lib/common/widgets/note_tile.dart +++ b/lib/common/widgets/note_tile.dart @@ -83,9 +83,7 @@ class _NoteTileState extends ConsumerState { onTap: _openOrSelect, onLongPress: widget.searchView ? null : _enterSelectionMode, child: Container( - // TODO: change when dynamic_color is updated (cf. https://github.com/material-foundation/flutter-packages/issues/574 and https://github.com/material-foundation/flutter-packages/issues/582) - color: - widget.note.selected ? Theme.of(context).colorScheme.surfaceVariant : null, // ignore: deprecated_member_use + color: widget.note.selected ? Theme.of(context).colorScheme.secondaryContainer : null, child: Padding( padding: Paddings.padding16.horizontal.add(Paddings.padding16.vertical), child: Row( diff --git a/lib/pages/editor/editor_page.dart b/lib/pages/editor/editor_page.dart index f9a25174..89439f56 100644 --- a/lib/pages/editor/editor_page.dart +++ b/lib/pages/editor/editor_page.dart @@ -131,8 +131,7 @@ class _EditorState extends ConsumerState { builder: (_, hasFocus, ___) { return showToolbar && hasFocus && KeyboardVisibilityProvider.isKeyboardVisible(context) ? ColoredBox( - // TODO: change when dynamic_color is updated (cf. https://github.com/material-foundation/flutter-packages/issues/574 and https://github.com/material-foundation/flutter-packages/issues/582) - color: Theme.of(context).colorScheme.surfaceVariant, // ignore: deprecated_member_use + color: Theme.of(context).colorScheme.secondaryContainer, child: EditorToolbar(fleatherController!), ) : Container(); diff --git a/lib/utils/theme_manager.dart b/lib/utils/theme_manager.dart index 7b17e420..876abbe6 100644 --- a/lib/utils/theme_manager.dart +++ b/lib/utils/theme_manager.dart @@ -83,11 +83,9 @@ class ThemeManager { if (useDynamicTheming && darkDynamicColorScheme != null) { colorScheme = useBlackTheming ? darkDynamicColorScheme.copyWith( - // TODO: remove when dynamic_color is updated - // (cf. https://github.com/material-foundation/flutter-packages/issues/574 - // and https://github.com/material-foundation/flutter-packages/issues/582) - background: useBlackTheming ? Colors.black : null, // ignore: deprecated_member_use - surface: useBlackTheming ? Colors.black : null, + // TODO: check if this is still needed to make the background of the pages black + background: Colors.black, // ignore: deprecated_member_use + surface: Colors.black, ) : darkDynamicColorScheme; } else {