Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve theming #66

Merged
merged 2 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class _AppState extends ConsumerState<App> with AfterLayoutMixin<App> {
@override
Widget build(BuildContext context) {
return DynamicColorBuilder(
builder: (lightDynamic, darkDynamic) {
builder: (lightDynamicColorScheme, darkDynamicColorScheme) {
return ValueListenableBuilder(
valueListenable: dynamicThemingNotifier,
builder: (_, __, ___) {
Expand All @@ -53,20 +53,12 @@ class _AppState extends ConsumerState<App> with AfterLayoutMixin<App> {
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,
Expand Down
4 changes: 1 addition & 3 deletions lib/common/widgets/note_tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ class _NoteTileState extends ConsumerState<NoteTile> {
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(
Expand Down
3 changes: 1 addition & 2 deletions lib/pages/editor/editor_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ class _EditorState extends ConsumerState<EditorPage> {
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();
Expand Down
94 changes: 45 additions & 49 deletions lib/utils/theme_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -15,27 +13,15 @@ class ThemeManager {

ThemeManager._internal();

final _customPrimaryColor = const Color(0xFF2278e9);

late final bool isDynamicThemingAvailable;

Future<void> init() async {
isDynamicThemingAvailable =
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<bool>();
}
Expand Down Expand Up @@ -78,6 +64,49 @@ 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: 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 {
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;
Expand All @@ -96,37 +125,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,
);
}
}
Loading