From 762b679eac80eade3d57ebec833885675f422bcc Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Tue, 4 Aug 2026 12:10:34 +0200 Subject: [PATCH] Only offer dynamic color where the platform supports it --- lib/core/consts.dart | 10 ++- .../account/widgets/settings/theme.dart | 21 +++--- lib/main.dart | 69 +++++++++++-------- lib/theme/dynamic_color.dart | 39 +++++++++++ .../account/widgets/settings_test.dart | 16 ++++- 5 files changed, 115 insertions(+), 40 deletions(-) create mode 100644 lib/theme/dynamic_color.dart diff --git a/lib/core/consts.dart b/lib/core/consts.dart index 157095a57..d7627dfde 100644 --- a/lib/core/consts.dart +++ b/lib/core/consts.dart @@ -59,9 +59,13 @@ const PREFS_USER_DARK_THEME = 'userDarkMode'; const PREFS_USER_LOCALE = 'userLocale'; const PREFS_LAST_SERVER = 'lastServer'; -/// User preference: use Material You dynamic colors from the platform (Android -/// 12+, iOS 18+, macOS 26, Windows/Linux accents) instead of the fixed wger -/// seed palette. Off by default. +/// User preference: use Material You dynamic colors from the platform instead +/// of the fixed wger seed palette. Off by 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'; /// Headless JWT auth: SharedPreferences keys. diff --git a/lib/features/account/widgets/settings/theme.dart b/lib/features/account/widgets/settings/theme.dart index 04bd94df1..6655f6017 100644 --- a/lib/features/account/widgets/settings/theme.dart +++ b/lib/features/account/widgets/settings/theme.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:wger/core/app_settings_notifier.dart'; import 'package:wger/l10n/generated/app_localizations.dart'; +import 'package:wger/theme/dynamic_color.dart'; class SettingsTheme extends ConsumerWidget { const SettingsTheme({super.key}); @@ -15,6 +16,9 @@ class SettingsTheme extends ConsumerWidget { final useDynamicColor = ref.watch( appSettingsProvider.select((s) => s.value?.useDynamicColor ?? false), ); + // Hidden while the probe is still running and where it comes back negative: + // the toggle does nothing on a platform without a dynamic palette. + final showDynamicColor = ref.watch(dynamicColorAvailableProvider).value ?? false; return Column( children: [ @@ -44,14 +48,15 @@ class SettingsTheme extends ConsumerWidget { }).toList(), ), ), - SwitchListTile( - key: const ValueKey('useDynamicColorSwitch'), - title: Text(i18n.useDynamicColor), - value: useDynamicColor, - onChanged: (bool value) { - ref.read(appSettingsProvider.notifier).setUseDynamicColor(value); - }, - ), + if (showDynamicColor) + SwitchListTile( + key: const ValueKey('useDynamicColorSwitch'), + title: Text(i18n.useDynamicColor), + value: useDynamicColor, + onChanged: (bool value) { + ref.read(appSettingsProvider.notifier).setUseDynamicColor(value); + }, + ), ], ); } diff --git a/lib/main.dart b/lib/main.dart index 25e84c4e1..04daee70e 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -187,36 +187,49 @@ class MainApp extends ConsumerWidget { // incoming deep links as a side effect of being read. ref.watch(appLinkRouterProvider); - return authAsync.when( - loading: () => const MaterialApp(home: SplashScreen()), - error: (error, stack) => MaterialApp( - localizationsDelegates: AppLocalizations.localizationsDelegates, - supportedLocales: AppLocalizations.supportedLocales, - home: AutoLoginErrorScreen(error: error), - ), - data: (authState) { - final themeMode = ref.watch( - appSettingsProvider.select((s) => s.value?.themeMode ?? ThemeMode.system), - ); - final userLocale = ref.watch( - appSettingsProvider.select((s) => s.value?.userLocale), - ); - final useDynamicColor = ref.watch( - appSettingsProvider.select((s) => s.value?.useDynamicColor ?? false), - ); + // Read before the builder below: its callback runs during the child's + // build, where ref.watch would be out of phase. + final themeMode = ref.watch( + appSettingsProvider.select((s) => s.value?.themeMode ?? ThemeMode.system), + ); + final userLocale = ref.watch( + appSettingsProvider.select((s) => s.value?.userLocale), + ); + final useDynamicColor = ref.watch( + appSettingsProvider.select((s) => s.value?.useDynamicColor ?? false), + ); - 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; + // Above the auth switch so the platform call is already in flight while the + // splash screen is up. Below it, the first frame after login renders the + // 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; + return authAsync.when( + loading: () => MaterialApp( + theme: light, + darkTheme: dark, + themeMode: themeMode, + home: const SplashScreen(), + ), + error: (error, stack) => MaterialApp( + theme: light, + darkTheme: dark, + themeMode: themeMode, + localizationsDelegates: AppLocalizations.localizationsDelegates, + supportedLocales: AppLocalizations.supportedLocales, + home: AutoLoginErrorScreen(error: error), + ), + data: (authState) { return MaterialApp( title: 'wger', navigatorKey: navigatorKey, diff --git a/lib/theme/dynamic_color.dart b/lib/theme/dynamic_color.dart new file mode 100644 index 000000000..8d730c823 --- /dev/null +++ b/lib/theme/dynamic_color.dart @@ -0,0 +1,39 @@ +/* + * This file is part of wger Workout Manager . + * 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 . + */ + +import 'package:dynamic_color/dynamic_color.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; + +/// Whether the platform can supply a dynamic color palette. +/// +/// True on Android 12+, which exposes a wallpaper palette, and on macOS, +/// Windows and GTK-based Linux, which expose a system accent color. False +/// everywhere else, including iOS, the web and Android below 12, where the +/// dynamic color setting would be a silent no-op. +final dynamicColorAvailableProvider = FutureProvider((ref) async { + try { + if (await DynamicColorPlugin.getCorePalette() != null) { + return true; + } + + return await DynamicColorPlugin.getAccentColor() != null; + } on PlatformException { + return false; + } +}); diff --git a/test/features/account/widgets/settings_test.dart b/test/features/account/widgets/settings_test.dart index a5c57480b..c3d1ae6d9 100644 --- a/test/features/account/widgets/settings_test.dart +++ b/test/features/account/widgets/settings_test.dart @@ -26,6 +26,7 @@ import 'package:wger/core/app_settings_notifier.dart'; import 'package:wger/core/consts.dart'; import 'package:wger/features/account/widgets/settings.dart'; import 'package:wger/l10n/generated/app_localizations.dart'; +import 'package:wger/theme/dynamic_color.dart'; import 'settings_test.mocks.dart'; @@ -49,10 +50,13 @@ void main() { ).thenAnswer((_) async {}); }); - Widget createSettingsScreen({locale = 'en'}) { + Widget createSettingsScreen({locale = 'en', bool dynamicColorAvailable = true}) { return riverpod.ProviderScope( overrides: [ appSettingsPrefsProvider.overrideWithValue(mockSharedPreferences), + // The plugin has no test implementation and would report the toggle as + // unsupported, which hides it. + dynamicColorAvailableProvider.overrideWith((ref) async => dynamicColorAvailable), ], child: MaterialApp( locale: Locale(locale), @@ -96,6 +100,16 @@ void main() { ); expect(switchWidget.value, true); }); + + testWidgets('dynamic color switch is hidden where the platform has none', ( + WidgetTester tester, + ) async { + await tester.pumpWidget(createSettingsScreen(dynamicColorAvailable: false)); + await tester.pumpAndSettle(); + + expect(find.byKey(const ValueKey('useDynamicColorSwitch')), findsNothing); + expect(find.byKey(const ValueKey('themeModeDropdown')), findsOneWidget); + }); }); group('Language switcher', () {