Only offer dynamic color where the platform supports it

This commit is contained in:
Roland Geider
2026-08-04 12:10:34 +02:00
parent a1f4844f6c
commit 762b679eac
5 changed files with 115 additions and 40 deletions

View File

@@ -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.

View File

@@ -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);
},
),
],
);
}

View File

@@ -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,

View File

@@ -0,0 +1,39 @@
/*
* 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: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<bool>((ref) async {
try {
if (await DynamicColorPlugin.getCorePalette() != null) {
return true;
}
return await DynamicColorPlugin.getAccentColor() != null;
} on PlatformException {
return false;
}
});

View File

@@ -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', () {