Apply dynamic color to the high contrast themes too

This commit is contained in:
Roland Geider
2026-08-04 12:19:52 +02:00
parent 762b679eac
commit 5c77a99bfc
10 changed files with 132 additions and 20 deletions

View File

@@ -91,7 +91,7 @@ sealed class AppSettings with _$AppSettings {
/// When true, the app palette follows the platform dynamic colors
/// (system wallpaper on Android 12+) instead of the fixed wger seeds.
/// A no-op on platforms without dynamic color support.
@Default(false) bool useDynamicColor,
@Default(USE_DYNAMIC_COLOR_DEFAULT) bool useDynamicColor,
}) = _AppSettings;
}
@@ -230,7 +230,7 @@ class AppSettingsNotifier extends _$AppSettingsNotifier {
//
Future<bool> _loadUseDynamicColor() async =>
(await _prefs.getBool(PREFS_USE_DYNAMIC_COLOR)) ?? false;
(await _prefs.getBool(PREFS_USE_DYNAMIC_COLOR)) ?? USE_DYNAMIC_COLOR_DEFAULT;
Future<void> setUseDynamicColor(bool value) async {
final current = state.asData?.value ?? const AppSettings();

View File

@@ -213,7 +213,7 @@ return $default(_that.themeMode,_that.dashboardItems,_that.userLocale,_that.keep
class _AppSettings implements AppSettings {
const _AppSettings({this.themeMode = ThemeMode.system, final List<DashboardItem> dashboardItems = const [], this.userLocale, this.keepDataOnLogout = KEEP_DATA_ON_LOGOUT_DEFAULT, this.allowSelfSignedCerts = ALLOW_SELF_SIGNED_CERTS_DEFAULT, this.useDynamicColor = false}): _dashboardItems = dashboardItems;
const _AppSettings({this.themeMode = ThemeMode.system, final List<DashboardItem> dashboardItems = const [], this.userLocale, this.keepDataOnLogout = KEEP_DATA_ON_LOGOUT_DEFAULT, this.allowSelfSignedCerts = ALLOW_SELF_SIGNED_CERTS_DEFAULT, this.useDynamicColor = USE_DYNAMIC_COLOR_DEFAULT}): _dashboardItems = dashboardItems;
@override@JsonKey() final ThemeMode themeMode;

View File

@@ -33,7 +33,7 @@ final class AppSettingsNotifierProvider
AppSettingsNotifier create() => AppSettingsNotifier();
}
String _$appSettingsNotifierHash() => r'309dcb1604e5cc002be4cff2b844a978388c190c';
String _$appSettingsNotifierHash() => r'71c64280cc0755452c9610247852a982be21a157';
abstract class _$AppSettingsNotifier extends $AsyncNotifier<AppSettings> {
FutureOr<AppSettings> build();

View File

@@ -60,13 +60,14 @@ const PREFS_USER_LOCALE = 'userLocale';
const PREFS_LAST_SERVER = 'lastServer';
/// User preference: use Material You dynamic colors from the platform instead
/// of the fixed wger seed palette. Off by default.
/// of the fixed wger seed palette. Defaults to [USE_DYNAMIC_COLOR_DEFAULT].
///
/// Android 12+ supplies a wallpaper palette, macOS (10.14+), Windows and
/// GTK-based Linux a system accent color. iOS and the web have no
/// implementation, so the setting is only offered where dynamicColorAvailable
/// reports true.
const PREFS_USE_DYNAMIC_COLOR = 'useDynamicColor';
const USE_DYNAMIC_COLOR_DEFAULT = false;
/// Headless JWT auth: SharedPreferences keys.
///

View File

@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:wger/core/app_settings_notifier.dart';
import 'package:wger/core/consts.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import 'package:wger/theme/dynamic_color.dart';
@@ -14,7 +15,7 @@ class SettingsTheme extends ConsumerWidget {
appSettingsProvider.select((s) => s.value?.themeMode ?? ThemeMode.system),
);
final useDynamicColor = ref.watch(
appSettingsProvider.select((s) => s.value?.useDynamicColor ?? false),
appSettingsProvider.select((s) => s.value?.useDynamicColor ?? USE_DYNAMIC_COLOR_DEFAULT),
);
// Hidden while the probe is still running and where it comes back negative:
// the toggle does nothing on a platform without a dynamic palette.

View File

@@ -71,6 +71,7 @@ import 'package:wger/features/routines/screens/settings_plates_screen.dart';
import 'package:wger/features/trophies/screens/trophy_screen.dart';
import 'package:wger/features/weight/screens/weight_screen.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import 'package:wger/theme/dynamic_color.dart';
import 'package:wger/theme/theme.dart';
void _setupLogging() {
@@ -196,7 +197,7 @@ class MainApp extends ConsumerWidget {
appSettingsProvider.select((s) => s.value?.userLocale),
);
final useDynamicColor = ref.watch(
appSettingsProvider.select((s) => s.value?.useDynamicColor ?? false),
appSettingsProvider.select((s) => s.value?.useDynamicColor ?? USE_DYNAMIC_COLOR_DEFAULT),
);
// Above the auth switch so the platform call is already in flight while the
@@ -204,15 +205,21 @@ class MainApp extends ConsumerWidget {
// fixed palette and then repaints.
return DynamicColorBuilder(
builder: (lightDynamic, darkDynamic) {
// One seed drives both themes: re-tonalising normalises the hue, so it
// doesn't matter which of the two schemes it comes from.
final seed = (lightDynamic ?? darkDynamic)?.primary;
final light = useDynamicColor && seed != null
? wgerThemeFromSeed(seed, Brightness.light)
: wgerLightTheme;
final dark = useDynamicColor && seed != null
? wgerThemeFromSeed(seed, Brightness.dark)
: wgerDarkTheme;
final seed = appThemeSeed(
useDynamicColor: useDynamicColor,
lightDynamic: lightDynamic,
darkDynamic: darkDynamic,
);
final light = seed == null ? wgerLightTheme : wgerThemeFromSeed(seed, Brightness.light);
final dark = seed == null ? wgerDarkTheme : wgerThemeFromSeed(seed, Brightness.dark);
// The OS accessibility setting swaps to these, so they follow the seed
// too rather than dropping back to the fixed palette.
final lightHc = seed == null
? wgerLightThemeHc
: wgerThemeFromSeed(seed, Brightness.light, highContrast: true);
final darkHc = seed == null
? wgerDarkThemeHc
: wgerThemeFromSeed(seed, Brightness.dark, highContrast: true);
return authAsync.when(
loading: () => MaterialApp(
@@ -236,8 +243,8 @@ class MainApp extends ConsumerWidget {
scaffoldMessengerKey: scaffoldMessengerKey,
theme: light,
darkTheme: dark,
highContrastTheme: wgerLightThemeHc,
highContrastDarkTheme: wgerDarkThemeHc,
highContrastTheme: lightHc,
highContrastDarkTheme: darkHc,
themeMode: themeMode,
locale: userLocale,
home: _getHomeScreen(authState),

View File

@@ -17,6 +17,7 @@
*/
import 'package:dynamic_color/dynamic_color.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
@@ -37,3 +38,20 @@ final dynamicColorAvailableProvider = FutureProvider<bool>((ref) async {
return false;
}
});
/// The color to seed the app themes from, or null to keep the fixed palette.
///
/// Null whenever the setting is off or the platform returned nothing. A single
/// seed drives both brightnesses: re-tonalising normalises the hue, so it does
/// not matter which of the two platform schemes it is taken from.
Color? appThemeSeed({
required bool useDynamicColor,
required ColorScheme? lightDynamic,
required ColorScheme? darkDynamic,
}) {
if (!useDynamicColor) {
return null;
}
return (lightDynamic ?? darkDynamic)?.primary;
}

View File

@@ -154,13 +154,20 @@ final wgerDarkThemeHc = FlexThemeData.dark(
/// Builds a wger theme for [brightness] with the palette generated from [seed],
/// e.g. the platform's dynamic color. Sub-themes, app bar style and typography
/// match the fixed themes, so only the hues change.
ThemeData wgerThemeFromSeed(Color seed, Brightness brightness) {
///
/// With [highContrast] the palette is spread over a wider tonal range, matching
/// the fixed high contrast themes the OS accessibility setting switches to.
ThemeData wgerThemeFromSeed(
Color seed,
Brightness brightness, {
bool highContrast = false,
}) {
final isDark = brightness == Brightness.dark;
return (isDark ? FlexThemeData.dark : FlexThemeData.light)(
colorScheme: SeedColorScheme.fromSeeds(
primaryKey: seed,
brightness: brightness,
tones: FlexTones.vivid(brightness),
tones: highContrast ? FlexTones.ultraContrast(brightness) : FlexTones.vivid(brightness),
),
useMaterial3: true,
appBarStyle: isDark ? null : FlexAppBarStyle.primary,

View File

@@ -0,0 +1,59 @@
/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* Copyright (c) 2026 wger Team
*
* wger Workout Manager is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:wger/theme/dynamic_color.dart';
void main() {
final light = ColorScheme.fromSeed(seedColor: const Color(0xFF6750A4));
final dark = ColorScheme.fromSeed(
seedColor: const Color(0xFFB3261E),
brightness: Brightness.dark,
);
group('appThemeSeed', () {
test('is null while the setting is off, whatever the platform offers', () {
expect(
appThemeSeed(useDynamicColor: false, lightDynamic: light, darkDynamic: dark),
isNull,
);
});
test('is null when the platform offers nothing', () {
expect(
appThemeSeed(useDynamicColor: true, lightDynamic: null, darkDynamic: null),
isNull,
);
});
test('prefers the light scheme when both are present', () {
expect(
appThemeSeed(useDynamicColor: true, lightDynamic: light, darkDynamic: dark),
light.primary,
);
});
test('falls back to the dark scheme on its own', () {
expect(
appThemeSeed(useDynamicColor: true, lightDynamic: null, darkDynamic: dark),
dark.primary,
);
});
});
}

View File

@@ -60,6 +60,25 @@ void main() {
}
});
test('high contrast spreads the palette further apart', () {
double contrast(Color a, Color b) {
final values = [a.computeLuminance(), b.computeLuminance()]..sort();
return (values.last + 0.05) / (values.first + 0.05);
}
for (final brightness in Brightness.values) {
final normal = wgerThemeFromSeed(seed, brightness).colorScheme;
final hc = wgerThemeFromSeed(seed, brightness, highContrast: true).colorScheme;
expect(
contrast(hc.onSurface, hc.surface),
greaterThan(contrast(normal.onSurface, normal.surface)),
reason: 'no extra contrast in $brightness',
);
}
});
test('keeps the wger typography', () {
final theme = wgerThemeFromSeed(seed, Brightness.light);