dynamic colors -> dynamic color

This commit is contained in:
Vadim 2023-01-23 21:39:55 +03:00
parent 7d7cab0e49
commit 5bcdfb2b1d
6 changed files with 31 additions and 31 deletions

View file

@ -1 +1 @@
enum DynamicColorsState { unavailable, enabled, disabled } enum DynamicColorState { unavailable, enabled, disabled }

View file

@ -10,7 +10,7 @@ class UserPreferencesService {
static const _hapticsKey = "haptics"; static const _hapticsKey = "haptics";
static const _themeTypeKey = "themeType"; static const _themeTypeKey = "themeType";
static const _dynamicColorsKey = "dynamicColors"; static const _dynamicColorKey = "dynamicColor";
final SharedPreferences _sharedPreferences; final SharedPreferences _sharedPreferences;
@ -28,6 +28,6 @@ class UserPreferencesService {
ThemeType get themeType => ThemeType.values[_sharedPreferences.getInt(_themeTypeKey) ?? 0]; ThemeType get themeType => ThemeType.values[_sharedPreferences.getInt(_themeTypeKey) ?? 0];
set themeType(ThemeType value) => _sharedPreferences.setInt(_themeTypeKey, value.index); set themeType(ThemeType value) => _sharedPreferences.setInt(_themeTypeKey, value.index);
bool get dynamicColors => _sharedPreferences.getBool(_dynamicColorsKey) ?? false; bool get dynamicColor => _sharedPreferences.getBool(_dynamicColorKey) ?? false;
set dynamicColors(bool value) => _sharedPreferences.setBool(_dynamicColorsKey, value); set dynamicColor(bool value) => _sharedPreferences.setBool(_dynamicColorKey, value);
} }

View file

@ -22,7 +22,7 @@
"haptics": "Haptics", "haptics": "Haptics",
"theme": "Theme", "theme": "Theme",
"chooseTheme": "Choose theme", "chooseTheme": "Choose theme",
"dynamicColors": "Dynamic colors", "dynamicColor": "Dynamic color",
"themeLight": "Light", "themeLight": "Light",
"themeDark": "Dark", "themeDark": "Dark",
"themeSystemDefault": "System default", "themeSystemDefault": "System default",

View file

@ -26,13 +26,13 @@ class ThemeProvider extends StatefulWidget {
class ThemeProviderState extends State<ThemeProvider> { class ThemeProviderState extends State<ThemeProvider> {
late final _themeTypeNotifier = ValueNotifier<ThemeType>(context.read<UserPreferencesService>().themeType); late final _themeTypeNotifier = ValueNotifier<ThemeType>(context.read<UserPreferencesService>().themeType);
late final _dynamicColorsNotifier = ValueNotifier<bool>(context.read<UserPreferencesService>().dynamicColors); late final _dynamicColorNotifier = ValueNotifier<bool>(context.read<UserPreferencesService>().dynamicColor);
late final _primaryColorNotifier = ValueNotifier<Color>(const Color(0xFF2196f3)); late final _primaryColorNotifier = ValueNotifier<Color>(const Color(0xFF2196f3));
@override @override
void dispose() { void dispose() {
_themeTypeNotifier.dispose(); _themeTypeNotifier.dispose();
_dynamicColorsNotifier.dispose(); _dynamicColorNotifier.dispose();
_primaryColorNotifier.dispose(); _primaryColorNotifier.dispose();
super.dispose(); super.dispose();
} }
@ -44,9 +44,9 @@ class ThemeProviderState extends State<ThemeProvider> {
builder: (_, themeType, __) => Provider.value( builder: (_, themeType, __) => Provider.value(
value: themeType, value: themeType,
child: ValueListenableBuilder( child: ValueListenableBuilder(
valueListenable: _dynamicColorsNotifier, valueListenable: _dynamicColorNotifier,
builder: (_, useDynamicColors, __) => _DynamicColorsProvider( builder: (_, useDynamicColor, __) => _DynamicColorProvider(
useDynamicColors: useDynamicColors, useDynamicColor: useDynamicColor,
themeBrightness: _themeBrightness, themeBrightness: _themeBrightness,
builder: (_, dynamicPrimaryColor) => ValueListenableBuilder( builder: (_, dynamicPrimaryColor) => ValueListenableBuilder(
valueListenable: _primaryColorNotifier, valueListenable: _primaryColorNotifier,
@ -78,19 +78,19 @@ class ThemeProviderState extends State<ThemeProvider> {
} }
} }
void enableDynamicColors(bool enable) { void enableDynamicColor(bool enable) {
_dynamicColorsNotifier.value = enable; _dynamicColorNotifier.value = enable;
context.read<UserPreferencesService>().dynamicColors = enable; context.read<UserPreferencesService>().dynamicColor = enable;
} }
} }
class _DynamicColorsProvider extends StatelessWidget { class _DynamicColorProvider extends StatelessWidget {
final bool useDynamicColors; final bool useDynamicColor;
final Brightness themeBrightness; final Brightness themeBrightness;
final Widget Function(BuildContext context, Color? primaryColor) builder; final Widget Function(BuildContext context, Color? primaryColor) builder;
const _DynamicColorsProvider({ const _DynamicColorProvider({
required this.useDynamicColors, required this.useDynamicColor,
required this.themeBrightness, required this.themeBrightness,
required this.builder, required this.builder,
}); });
@ -99,19 +99,19 @@ class _DynamicColorsProvider extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return DynamicColorBuilder( return DynamicColorBuilder(
builder: (lightDynamic, darkDynamic) { builder: (lightDynamic, darkDynamic) {
late final DynamicColorsState state; late final DynamicColorState state;
late final Color? dynamicPrimaryColor; late final Color? dynamicPrimaryColor;
if (lightDynamic != null && darkDynamic != null) { if (lightDynamic != null && darkDynamic != null) {
if (useDynamicColors) { if (useDynamicColor) {
dynamicPrimaryColor = (themeBrightness == Brightness.light ? lightDynamic : darkDynamic).primary; dynamicPrimaryColor = (themeBrightness == Brightness.light ? lightDynamic : darkDynamic).primary;
state = DynamicColorsState.enabled; state = DynamicColorState.enabled;
} else { } else {
dynamicPrimaryColor = null; dynamicPrimaryColor = null;
state = DynamicColorsState.disabled; state = DynamicColorState.disabled;
} }
} else { } else {
dynamicPrimaryColor = null; dynamicPrimaryColor = null;
state = DynamicColorsState.unavailable; state = DynamicColorState.unavailable;
} }
return Provider.value( return Provider.value(
value: state, value: state,

View file

@ -4,18 +4,18 @@ import 'package:lightmeter/data/models/dynamic_colors_state.dart';
import 'package:lightmeter/generated/l10n.dart'; import 'package:lightmeter/generated/l10n.dart';
import 'package:lightmeter/res/theme.dart'; import 'package:lightmeter/res/theme.dart';
class DynamicColorsListTile extends StatelessWidget { class DynamicColorListTile extends StatelessWidget {
const DynamicColorsListTile({super.key}); const DynamicColorListTile({super.key});
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (context.read<DynamicColorsState>() == DynamicColorsState.unavailable) { if (context.read<DynamicColorState>() == DynamicColorState.unavailable) {
return Opacity( return Opacity(
opacity: 0.5, opacity: 0.5,
child: IgnorePointer( child: IgnorePointer(
child: SwitchListTile( child: SwitchListTile(
secondary: const Icon(Icons.colorize), secondary: const Icon(Icons.colorize),
title: Text(S.of(context).dynamicColors), title: Text(S.of(context).dynamicColor),
value: false, value: false,
enableFeedback: false, enableFeedback: false,
onChanged: (value) {}, onChanged: (value) {},
@ -25,9 +25,9 @@ class DynamicColorsListTile extends StatelessWidget {
} }
return SwitchListTile( return SwitchListTile(
secondary: const Icon(Icons.colorize), secondary: const Icon(Icons.colorize),
title: Text(S.of(context).dynamicColors), title: Text(S.of(context).dynamicColor),
value: context.watch<DynamicColorsState>() == DynamicColorsState.enabled, value: context.watch<DynamicColorState>() == DynamicColorState.enabled,
onChanged: ThemeProvider.of(context).enableDynamicColors, onChanged: ThemeProvider.of(context).enableDynamicColor,
); );
} }
} }

View file

@ -6,7 +6,7 @@ import 'components/haptics/provider_list_tile_haptics.dart';
import 'components/report_issue/widget_list_tile_report_issue.dart'; import 'components/report_issue/widget_list_tile_report_issue.dart';
import 'components/shared/settings_section/widget_settings_section.dart'; import 'components/shared/settings_section/widget_settings_section.dart';
import 'components/source_code/widget_list_tile_source_code.dart'; import 'components/source_code/widget_list_tile_source_code.dart';
import 'components/dynamic_colors/widget_list_tile_dynamic_colors.dart'; import 'components/dynamic_color/widget_list_tile_dynamic_color.dart';
import 'components/theme_type/widget_list_tile_theme_type.dart'; import 'components/theme_type/widget_list_tile_theme_type.dart';
import 'components/version/widget_list_tile_version.dart'; import 'components/version/widget_list_tile_version.dart';
import 'components/fractional_stops/widget_list_tile_fractional_stops.dart'; import 'components/fractional_stops/widget_list_tile_fractional_stops.dart';
@ -60,7 +60,7 @@ class SettingsScreen extends StatelessWidget {
title: S.of(context).theme, title: S.of(context).theme,
children: const [ children: const [
ThemeTypeListTile(), ThemeTypeListTile(),
DynamicColorsListTile(), DynamicColorListTile(),
], ],
), ),
SettingsSection( SettingsSection(