From d2d57f5fb9cf19a32b0a0c215003289d91427339 Mon Sep 17 00:00:00 2001 From: Vadim Date: Sun, 13 Aug 2023 16:46:13 +0200 Subject: [PATCH] Moved `ThemeProvider` functionality to `EnumProviders` --- lib/application.dart | 90 +++---- lib/providers.dart | 7 +- lib/providers/enum_providers.dart | 125 ++++++++- lib/providers/theme_provider.dart | 246 ------------------ lib/res/theme.dart | 97 +++++++ .../widget_list_tile_dynamic_color.dart | 7 +- .../widget_dialog_picker_primary_color.dart | 6 +- .../widget_list_tile_primary_color.dart | 7 +- .../theme/widget_settings_section_theme.dart | 4 +- test/data/shared_prefs_service_test.dart | 6 +- 10 files changed, 271 insertions(+), 324 deletions(-) delete mode 100644 lib/providers/theme_provider.dart create mode 100644 lib/res/theme.dart diff --git a/lib/application.dart b/lib/application.dart index 6bae96d..b675b4b 100644 --- a/lib/application.dart +++ b/lib/application.dart @@ -6,9 +6,9 @@ import 'package:lightmeter/environment.dart'; import 'package:lightmeter/generated/l10n.dart'; import 'package:lightmeter/providers.dart'; import 'package:lightmeter/providers/enum_providers.dart'; +import 'package:lightmeter/res/theme.dart'; import 'package:lightmeter/screens/metering/flow_metering.dart'; import 'package:lightmeter/screens/settings/flow_settings.dart'; -import 'package:lightmeter/utils/inherited_generics.dart'; class Application extends StatelessWidget { final Environment env; @@ -19,54 +19,48 @@ class Application extends StatelessWidget { Widget build(BuildContext context) { return LightmeterProviders( env: env, - builder: (context, ready) => ready - ? _AnnotatedRegionWrapper( - child: MaterialApp( - theme: context.listen(), - locale: Locale(EnumProviders.localeOf(context).intlName), - localizationsDelegates: const [ - S.delegate, - GlobalMaterialLocalizations.delegate, - GlobalWidgetsLocalizations.delegate, - GlobalCupertinoLocalizations.delegate, - ], - supportedLocales: S.delegate.supportedLocales, - builder: (context, child) => MediaQuery( - data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0), - child: child!, - ), - initialRoute: "metering", - routes: { - "metering": (context) => const MeteringFlow(), - "settings": (context) => const SettingsFlow(), - }, + builder: (context, ready) { + if (ready) { + final theme = themeFrom( + EnumProviders.primaryColorOf(context), + EnumProviders.brightnessOf(context), + ); + final systemIconsBrightness = + ThemeData.estimateBrightnessForColor(theme.colorScheme.onSurface); + return AnnotatedRegion( + value: SystemUiOverlayStyle( + statusBarColor: Colors.transparent, + statusBarBrightness: + systemIconsBrightness == Brightness.light ? Brightness.dark : Brightness.light, + statusBarIconBrightness: systemIconsBrightness, + systemNavigationBarColor: Colors.transparent, + systemNavigationBarIconBrightness: systemIconsBrightness, + ), + child: MaterialApp( + theme: theme, + locale: Locale(EnumProviders.localeOf(context).intlName), + localizationsDelegates: const [ + S.delegate, + GlobalMaterialLocalizations.delegate, + GlobalWidgetsLocalizations.delegate, + GlobalCupertinoLocalizations.delegate, + ], + supportedLocales: S.delegate.supportedLocales, + builder: (context, child) => MediaQuery( + data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0), + child: child!, ), - ) - : const SizedBox(), - ); - } -} - -class _AnnotatedRegionWrapper extends StatelessWidget { - final Widget child; - - const _AnnotatedRegionWrapper({required this.child}); - - @override - Widget build(BuildContext context) { - final systemIconsBrightness = ThemeData.estimateBrightnessForColor( - context.listen().colorScheme.onSurface, - ); - return AnnotatedRegion( - value: SystemUiOverlayStyle( - statusBarColor: Colors.transparent, - statusBarBrightness: - systemIconsBrightness == Brightness.light ? Brightness.dark : Brightness.light, - statusBarIconBrightness: systemIconsBrightness, - systemNavigationBarColor: Colors.transparent, - systemNavigationBarIconBrightness: systemIconsBrightness, - ), - child: child, + initialRoute: "metering", + routes: { + "metering": (context) => const MeteringFlow(), + "settings": (context) => const SettingsFlow(), + }, + ), + ); + } else { + return const SizedBox(); + } + }, ); } } diff --git a/lib/providers.dart b/lib/providers.dart index cb26523..c4d893b 100644 --- a/lib/providers.dart +++ b/lib/providers.dart @@ -10,7 +10,6 @@ import 'package:lightmeter/providers/enum_providers.dart'; import 'package:lightmeter/providers/equipment_profile_provider.dart'; import 'package:lightmeter/providers/metering_screen_layout_provider.dart'; import 'package:lightmeter/providers/service_providers.dart'; -import 'package:lightmeter/providers/theme_provider.dart'; import 'package:platform/platform.dart'; import 'package:shared_preferences/shared_preferences.dart'; @@ -40,10 +39,8 @@ class LightmeterProviders extends StatelessWidget { child: EnumProviders( child: MeteringScreenLayoutProvider( child: EquipmentProfileProvider( - child: ThemeProvider( - child: Builder( - builder: (context) => builder(context, true), - ), + child: Builder( + builder: (context) => builder(context, true), ), ), ), diff --git a/lib/providers/enum_providers.dart b/lib/providers/enum_providers.dart index c37ff86..3710217 100644 --- a/lib/providers/enum_providers.dart +++ b/lib/providers/enum_providers.dart @@ -1,4 +1,7 @@ +import 'package:dynamic_color/dynamic_color.dart'; import 'package:flutter/material.dart'; +import 'package:flutter/scheduler.dart'; +import 'package:lightmeter/data/models/dynamic_colors_state.dart'; import 'package:lightmeter/data/models/ev_source_type.dart'; import 'package:lightmeter/data/models/supported_locale.dart'; import 'package:lightmeter/data/models/theme_type.dart'; @@ -8,8 +11,11 @@ import 'package:lightmeter/providers/service_providers.dart'; import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; enum _ListenableAspect { + brightness, + dynamicColorState, evSourceType, locale, + primaryColor, stopType, themeType, } @@ -23,6 +29,22 @@ class EnumProviders extends StatefulWidget { return context.findAncestorStateOfType<_EnumProvidersState>()!; } + static Brightness brightnessOf(BuildContext context) { + return InheritedModel.inheritFrom<_EnumProvidersModel>( + context, + aspect: _ListenableAspect.brightness, + )! + .brightness; + } + + static DynamicColorState dynamicColorStateOf(BuildContext context) { + return InheritedModel.inheritFrom<_EnumProvidersModel>( + context, + aspect: _ListenableAspect.dynamicColorState, + )! + .dynamicColorState; + } + static EvSourceType evSourceTypeOf(BuildContext context) { return InheritedModel.inheritFrom<_EnumProvidersModel>( context, @@ -39,6 +61,14 @@ class EnumProviders extends StatefulWidget { .locale; } + static Color primaryColorOf(BuildContext context) { + return InheritedModel.inheritFrom<_EnumProvidersModel>( + context, + aspect: _ListenableAspect.primaryColor, + )! + .primaryColor; + } + static StopType stopTypeOf(BuildContext context) { return InheritedModel.inheritFrom<_EnumProvidersModel>( context, @@ -59,11 +89,13 @@ class EnumProviders extends StatefulWidget { State createState() => _EnumProvidersState(); } -class _EnumProvidersState extends State { +class _EnumProvidersState extends State with WidgetsBindingObserver { UserPreferencesService get userPreferencesService => ServiceProviders.userPreferencesServiceOf(context); + late bool dynamicColor = userPreferencesService.dynamicColor; late EvSourceType evSourceType; + late Color primaryColor = userPreferencesService.primaryColor; late StopType stopType = userPreferencesService.stopType; late SupportedLocale locale = userPreferencesService.locale; late ThemeType themeType = userPreferencesService.themeType; @@ -76,19 +108,61 @@ class _EnumProvidersState extends State { !ServiceProviders.environmentOf(context).hasLightSensor ? EvSourceType.camera : evSourceType; + WidgetsBinding.instance.addObserver(this); + } + + @override + void didChangePlatformBrightness() { + super.didChangePlatformBrightness(); + setState(() {}); + } + + @override + void dispose() { + WidgetsBinding.instance.removeObserver(this); + super.dispose(); } @override Widget build(BuildContext context) { - return _EnumProvidersModel( - evSourceType: evSourceType, - locale: locale, - stopType: stopType, - themeType: themeType, - child: widget.child, + return DynamicColorBuilder( + builder: (lightDynamic, darkDynamic) { + late final DynamicColorState state; + late final Color? dynamicPrimaryColor; + if (lightDynamic != null && darkDynamic != null) { + if (dynamicColor) { + dynamicPrimaryColor = + (_themeBrightness == Brightness.light ? lightDynamic : darkDynamic).primary; + state = DynamicColorState.enabled; + } else { + dynamicPrimaryColor = null; + state = DynamicColorState.disabled; + } + } else { + dynamicPrimaryColor = null; + state = DynamicColorState.unavailable; + } + return _EnumProvidersModel( + brightness: _themeBrightness, + dynamicColorState: state, + evSourceType: evSourceType, + locale: locale, + primaryColor: dynamicPrimaryColor ?? primaryColor, + stopType: stopType, + themeType: themeType, + child: widget.child, + ); + }, ); } + void enableDynamicColor(bool enable) { + setState(() { + dynamicColor = enable; + }); + ServiceProviders.userPreferencesServiceOf(context).dynamicColor = enable; + } + void toggleEvSourceType() { if (!ServiceProviders.environmentOf(context).hasLightSensor) { return; @@ -113,6 +187,13 @@ class _EnumProvidersState extends State { }); } + void setPrimaryColor(Color primaryColor) { + setState(() { + this.primaryColor = primaryColor; + }); + ServiceProviders.userPreferencesServiceOf(context).primaryColor = primaryColor; + } + void setStopType(StopType stopType) { setState(() { this.stopType = stopType; @@ -126,17 +207,34 @@ class _EnumProvidersState extends State { }); ServiceProviders.userPreferencesServiceOf(context).themeType = themeType; } + + Brightness get _themeBrightness { + switch (themeType) { + case ThemeType.light: + return Brightness.light; + case ThemeType.dark: + return Brightness.dark; + case ThemeType.systemDefault: + return SchedulerBinding.instance.platformDispatcher.platformBrightness; + } + } } class _EnumProvidersModel extends InheritedModel<_ListenableAspect> { + final Brightness brightness; + final DynamicColorState dynamicColorState; final EvSourceType evSourceType; final SupportedLocale locale; + final Color primaryColor; final StopType stopType; final ThemeType themeType; const _EnumProvidersModel({ + required this.brightness, + required this.dynamicColorState, required this.evSourceType, required this.locale, + required this.primaryColor, required this.stopType, required this.themeType, required super.child, @@ -144,8 +242,11 @@ class _EnumProvidersModel extends InheritedModel<_ListenableAspect> { @override bool updateShouldNotify(_EnumProvidersModel oldWidget) { - return evSourceType != oldWidget.evSourceType || + return brightness != oldWidget.brightness || + dynamicColorState != oldWidget.dynamicColorState || + evSourceType != oldWidget.evSourceType || locale != oldWidget.locale || + primaryColor != oldWidget.primaryColor || stopType != oldWidget.stopType || themeType != oldWidget.themeType; } @@ -155,9 +256,15 @@ class _EnumProvidersModel extends InheritedModel<_ListenableAspect> { _EnumProvidersModel oldWidget, Set<_ListenableAspect> dependencies, ) { - return (evSourceType != oldWidget.evSourceType && + return (brightness != oldWidget.brightness && + dependencies.contains(_ListenableAspect.brightness)) || + (dynamicColorState != oldWidget.dynamicColorState && + dependencies.contains(_ListenableAspect.dynamicColorState)) || + (evSourceType != oldWidget.evSourceType && dependencies.contains(_ListenableAspect.evSourceType)) || (locale != oldWidget.locale && dependencies.contains(_ListenableAspect.locale)) || + (primaryColor != oldWidget.primaryColor && + dependencies.contains(_ListenableAspect.primaryColor)) || (stopType != oldWidget.stopType && dependencies.contains(_ListenableAspect.stopType)) || (themeType != oldWidget.themeType && dependencies.contains(_ListenableAspect.themeType)); } diff --git a/lib/providers/theme_provider.dart b/lib/providers/theme_provider.dart deleted file mode 100644 index df6811b..0000000 --- a/lib/providers/theme_provider.dart +++ /dev/null @@ -1,246 +0,0 @@ -import 'package:dynamic_color/dynamic_color.dart'; -import 'package:flutter/material.dart'; -import 'package:flutter/scheduler.dart'; -import 'package:lightmeter/data/models/dynamic_colors_state.dart'; -import 'package:lightmeter/data/models/theme_type.dart'; -import 'package:lightmeter/data/shared_prefs_service.dart'; -import 'package:lightmeter/providers/enum_providers.dart'; -import 'package:lightmeter/providers/service_providers.dart'; -import 'package:lightmeter/res/dimens.dart'; -import 'package:lightmeter/utils/inherited_generics.dart'; -import 'package:material_color_utilities/material_color_utilities.dart'; - -class ThemeProvider extends StatefulWidget { - final Widget child; - - const ThemeProvider({ - required this.child, - super.key, - }); - - static ThemeProviderState of(BuildContext context) { - return context.findAncestorStateOfType()!; - } - - static const primaryColorsList = [ - Color(0xfff44336), - Color(0xffe91e63), - Color(0xff9c27b0), - Color(0xff673ab7), - Color(0xff3f51b5), - Color(0xff2196f3), - Color(0xff03a9f4), - Color(0xff00bcd4), - Color(0xff009688), - Color(0xff4caf50), - Color(0xff8bc34a), - Color(0xffcddc39), - Color(0xffffeb3b), - Color(0xffffc107), - Color(0xffff9800), - Color(0xffff5722), - ]; - - @override - State createState() => ThemeProviderState(); -} - -class ThemeProviderState extends State with WidgetsBindingObserver { - UserPreferencesService get _prefs => ServiceProviders.userPreferencesServiceOf(context); - - late final _dynamicColorNotifier = ValueNotifier(_prefs.dynamicColor); - late final _primaryColorNotifier = ValueNotifier(_prefs.primaryColor); - - @override - void initState() { - super.initState(); - WidgetsBinding.instance.addObserver(this); - } - - @override - void didChangePlatformBrightness() { - super.didChangePlatformBrightness(); - setState(() {}); - } - - @override - void dispose() { - WidgetsBinding.instance.removeObserver(this); - _dynamicColorNotifier.dispose(); - _primaryColorNotifier.dispose(); - super.dispose(); - } - - @override - Widget build(BuildContext context) { - return ValueListenableBuilder( - valueListenable: _dynamicColorNotifier, - builder: (_, useDynamicColor, __) => _DynamicColorProvider( - useDynamicColor: useDynamicColor, - themeBrightness: _themeBrightness, - builder: (_, dynamicPrimaryColor) => ValueListenableBuilder( - valueListenable: _primaryColorNotifier, - builder: (_, primaryColor, __) => _ThemeDataProvider( - primaryColor: dynamicPrimaryColor ?? primaryColor, - brightness: _themeBrightness, - child: widget.child, - ), - ), - ), - ); - } - - Brightness get _themeBrightness { - switch (EnumProviders.themeTypeOf(context)) { - case ThemeType.light: - return Brightness.light; - case ThemeType.dark: - return Brightness.dark; - case ThemeType.systemDefault: - return SchedulerBinding.instance.platformDispatcher.platformBrightness; - } - } - - void setPrimaryColor(Color color) { - _primaryColorNotifier.value = color; - _prefs.primaryColor = color; - } - - void enableDynamicColor(bool enable) { - _dynamicColorNotifier.value = enable; - _prefs.dynamicColor = enable; - } -} - -class _DynamicColorProvider extends StatelessWidget { - final bool useDynamicColor; - final Brightness themeBrightness; - final Widget Function(BuildContext context, Color? primaryColor) builder; - - const _DynamicColorProvider({ - required this.useDynamicColor, - required this.themeBrightness, - required this.builder, - }); - - @override - Widget build(BuildContext context) { - return DynamicColorBuilder( - builder: (lightDynamic, darkDynamic) { - late final DynamicColorState state; - late final Color? dynamicPrimaryColor; - if (lightDynamic != null && darkDynamic != null) { - if (useDynamicColor) { - dynamicPrimaryColor = - (themeBrightness == Brightness.light ? lightDynamic : darkDynamic).primary; - state = DynamicColorState.enabled; - } else { - dynamicPrimaryColor = null; - state = DynamicColorState.disabled; - } - } else { - dynamicPrimaryColor = null; - state = DynamicColorState.unavailable; - } - return InheritedWidgetBase( - data: state, - child: builder(context, dynamicPrimaryColor), - ); - }, - ); - } -} - -class _ThemeDataProvider extends StatelessWidget { - final Color primaryColor; - final Brightness brightness; - final Widget child; - - const _ThemeDataProvider({ - required this.primaryColor, - required this.brightness, - required this.child, - }); - - @override - Widget build(BuildContext context) { - return InheritedWidgetBase( - data: _themeFromColorScheme(_colorSchemeFromColor()), - child: child, - ); - } - - ThemeData _themeFromColorScheme(ColorScheme scheme) { - return ThemeData( - useMaterial3: true, - brightness: scheme.brightness, - primaryColor: primaryColor, - colorScheme: scheme, - appBarTheme: AppBarTheme( - elevation: 4, - color: scheme.surface, - surfaceTintColor: scheme.surfaceTint, - ), - cardTheme: CardTheme( - clipBehavior: Clip.antiAlias, - color: scheme.surface, - elevation: 4, - margin: EdgeInsets.zero, - shadowColor: Colors.transparent, - shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(Dimens.borderRadiusL)), - surfaceTintColor: scheme.surfaceTint, - ), - dialogBackgroundColor: scheme.surface, - dialogTheme: DialogTheme( - backgroundColor: scheme.surface, - surfaceTintColor: scheme.surfaceTint, - elevation: 6, - ), - dividerColor: scheme.outlineVariant, - dividerTheme: DividerThemeData( - color: scheme.outlineVariant, - space: 0, - ), - listTileTheme: ListTileThemeData( - style: ListTileStyle.list, - iconColor: scheme.onSurface, - textColor: scheme.onSurface, - ), - scaffoldBackgroundColor: scheme.surface, - ); - } - - ColorScheme _colorSchemeFromColor() { - final scheme = brightness == Brightness.light - ? Scheme.light(primaryColor.value) - : Scheme.dark(primaryColor.value); - - return ColorScheme( - brightness: brightness, - background: Color(scheme.background), - error: Color(scheme.error), - errorContainer: Color(scheme.errorContainer), - onBackground: Color(scheme.onBackground), - onError: Color(scheme.onError), - onErrorContainer: Color(scheme.onErrorContainer), - primary: Color(scheme.primary), - onPrimary: Color(scheme.onPrimary), - primaryContainer: Color(scheme.primaryContainer), - onPrimaryContainer: Color(scheme.onPrimaryContainer), - secondary: Color(scheme.secondary), - onSecondary: Color(scheme.onSecondary), - surface: Color.alphaBlend( - Color(scheme.primary).withOpacity(0.05), - Color(scheme.background), - ), - onSurface: Color(scheme.onSurface), - surfaceVariant: Color.alphaBlend( - Color(scheme.primary).withOpacity(0.5), - Color(scheme.background), - ), - onSurfaceVariant: Color(scheme.onSurfaceVariant), - outline: Color(scheme.outline), - outlineVariant: Color(scheme.outlineVariant), - ); - } -} diff --git a/lib/res/theme.dart b/lib/res/theme.dart new file mode 100644 index 0000000..a6320c1 --- /dev/null +++ b/lib/res/theme.dart @@ -0,0 +1,97 @@ +import 'package:flutter/material.dart'; +import 'package:lightmeter/res/dimens.dart'; +import 'package:material_color_utilities/material_color_utilities.dart'; + +const primaryColorsList = [ + Color(0xfff44336), + Color(0xffe91e63), + Color(0xff9c27b0), + Color(0xff673ab7), + Color(0xff3f51b5), + Color(0xff2196f3), + Color(0xff03a9f4), + Color(0xff00bcd4), + Color(0xff009688), + Color(0xff4caf50), + Color(0xff8bc34a), + Color(0xffcddc39), + Color(0xffffeb3b), + Color(0xffffc107), + Color(0xffff9800), + Color(0xffff5722), +]; + +ThemeData themeFrom(Color primaryColor, Brightness brightness) { + final scheme = _colorSchemeFromColor(primaryColor, brightness); + return ThemeData( + useMaterial3: true, + brightness: scheme.brightness, + primaryColor: primaryColor, + colorScheme: scheme, + appBarTheme: AppBarTheme( + elevation: 4, + color: scheme.surface, + surfaceTintColor: scheme.surfaceTint, + ), + cardTheme: CardTheme( + clipBehavior: Clip.antiAlias, + color: scheme.surface, + elevation: 4, + margin: EdgeInsets.zero, + shadowColor: Colors.transparent, + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(Dimens.borderRadiusL)), + surfaceTintColor: scheme.surfaceTint, + ), + dialogBackgroundColor: scheme.surface, + dialogTheme: DialogTheme( + backgroundColor: scheme.surface, + surfaceTintColor: scheme.surfaceTint, + elevation: 6, + ), + dividerColor: scheme.outlineVariant, + dividerTheme: DividerThemeData( + color: scheme.outlineVariant, + space: 0, + ), + listTileTheme: ListTileThemeData( + style: ListTileStyle.list, + iconColor: scheme.onSurface, + textColor: scheme.onSurface, + ), + scaffoldBackgroundColor: scheme.surface, + ); +} + +ColorScheme _colorSchemeFromColor(Color primaryColor, Brightness brightness) { + final scheme = brightness == Brightness.light + ? Scheme.light(primaryColor.value) + : Scheme.dark(primaryColor.value); + + return ColorScheme( + brightness: brightness, + background: Color(scheme.background), + error: Color(scheme.error), + errorContainer: Color(scheme.errorContainer), + onBackground: Color(scheme.onBackground), + onError: Color(scheme.onError), + onErrorContainer: Color(scheme.onErrorContainer), + primary: Color(scheme.primary), + onPrimary: Color(scheme.onPrimary), + primaryContainer: Color(scheme.primaryContainer), + onPrimaryContainer: Color(scheme.onPrimaryContainer), + secondary: Color(scheme.secondary), + onSecondary: Color(scheme.onSecondary), + surface: Color.alphaBlend( + Color(scheme.primary).withOpacity(0.05), + Color(scheme.background), + ), + onSurface: Color(scheme.onSurface), + surfaceVariant: Color.alphaBlend( + Color(scheme.primary).withOpacity(0.5), + Color(scheme.background), + ), + onSurfaceVariant: Color(scheme.onSurfaceVariant), + outline: Color(scheme.outline), + outlineVariant: Color(scheme.outlineVariant), + ); +} diff --git a/lib/screens/settings/components/theme/components/dynamic_color/widget_list_tile_dynamic_color.dart b/lib/screens/settings/components/theme/components/dynamic_color/widget_list_tile_dynamic_color.dart index d230f63..c4627d9 100644 --- a/lib/screens/settings/components/theme/components/dynamic_color/widget_list_tile_dynamic_color.dart +++ b/lib/screens/settings/components/theme/components/dynamic_color/widget_list_tile_dynamic_color.dart @@ -1,9 +1,8 @@ import 'package:flutter/material.dart'; import 'package:lightmeter/data/models/dynamic_colors_state.dart'; import 'package:lightmeter/generated/l10n.dart'; -import 'package:lightmeter/providers/theme_provider.dart'; +import 'package:lightmeter/providers/enum_providers.dart'; import 'package:lightmeter/res/dimens.dart'; -import 'package:lightmeter/utils/inherited_generics.dart'; class DynamicColorListTile extends StatelessWidget { const DynamicColorListTile({super.key}); @@ -13,8 +12,8 @@ class DynamicColorListTile extends StatelessWidget { return SwitchListTile( secondary: const Icon(Icons.colorize), title: Text(S.of(context).dynamicColor), - value: context.listen() == DynamicColorState.enabled, - onChanged: ThemeProvider.of(context).enableDynamicColor, + value: EnumProviders.dynamicColorStateOf(context) == DynamicColorState.enabled, + onChanged: EnumProviders.of(context).enableDynamicColor, contentPadding: const EdgeInsets.symmetric(horizontal: Dimens.paddingM), ); } diff --git a/lib/screens/settings/components/theme/components/primary_color/components/primary_color_picker_dialog/widget_dialog_picker_primary_color.dart b/lib/screens/settings/components/theme/components/primary_color/components/primary_color_picker_dialog/widget_dialog_picker_primary_color.dart index 434336a..380faa1 100644 --- a/lib/screens/settings/components/theme/components/primary_color/components/primary_color_picker_dialog/widget_dialog_picker_primary_color.dart +++ b/lib/screens/settings/components/theme/components/primary_color/components/primary_color_picker_dialog/widget_dialog_picker_primary_color.dart @@ -1,7 +1,7 @@ import 'package:flutter/material.dart'; import 'package:lightmeter/generated/l10n.dart'; -import 'package:lightmeter/providers/theme_provider.dart'; import 'package:lightmeter/res/dimens.dart'; +import 'package:lightmeter/res/theme.dart'; import 'package:lightmeter/screens/shared/filled_circle/widget_circle_filled.dart'; class PrimaryColorDialogPicker extends StatefulWidget { @@ -38,9 +38,9 @@ class _PrimaryColorDialogPickerState extends State { padding: EdgeInsets.zero, child: Row( children: List.generate( - ThemeProvider.primaryColorsList.length, + primaryColorsList.length, (index) { - final color = ThemeProvider.primaryColorsList[index]; + final color = primaryColorsList[index]; return Padding( padding: EdgeInsets.only(left: index == 0 ? 0 : Dimens.paddingS), child: _SelectableColorItem( diff --git a/lib/screens/settings/components/theme/components/primary_color/widget_list_tile_primary_color.dart b/lib/screens/settings/components/theme/components/primary_color/widget_list_tile_primary_color.dart index afc1a7b..c8f7074 100644 --- a/lib/screens/settings/components/theme/components/primary_color/widget_list_tile_primary_color.dart +++ b/lib/screens/settings/components/theme/components/primary_color/widget_list_tile_primary_color.dart @@ -1,17 +1,16 @@ import 'package:flutter/material.dart'; import 'package:lightmeter/data/models/dynamic_colors_state.dart'; import 'package:lightmeter/generated/l10n.dart'; -import 'package:lightmeter/providers/theme_provider.dart'; +import 'package:lightmeter/providers/enum_providers.dart'; import 'package:lightmeter/res/dimens.dart'; import 'package:lightmeter/screens/settings/components/theme/components/primary_color/components/primary_color_picker_dialog/widget_dialog_picker_primary_color.dart'; -import 'package:lightmeter/utils/inherited_generics.dart'; class PrimaryColorListTile extends StatelessWidget { const PrimaryColorListTile({super.key}); @override Widget build(BuildContext context) { - if (context.listen() == DynamicColorState.enabled) { + if (EnumProviders.dynamicColorStateOf(context) == DynamicColorState.enabled) { return Opacity( opacity: Dimens.disabledOpacity, child: IgnorePointer( @@ -31,7 +30,7 @@ class PrimaryColorListTile extends StatelessWidget { builder: (_) => const PrimaryColorDialogPicker(), ).then((value) { if (value != null) { - ThemeProvider.of(context).setPrimaryColor(value); + EnumProviders.of(context).setPrimaryColor(value); } }); }, diff --git a/lib/screens/settings/components/theme/widget_settings_section_theme.dart b/lib/screens/settings/components/theme/widget_settings_section_theme.dart index d137469..5b669f4 100644 --- a/lib/screens/settings/components/theme/widget_settings_section_theme.dart +++ b/lib/screens/settings/components/theme/widget_settings_section_theme.dart @@ -1,11 +1,11 @@ import 'package:flutter/material.dart'; import 'package:lightmeter/data/models/dynamic_colors_state.dart'; import 'package:lightmeter/generated/l10n.dart'; +import 'package:lightmeter/providers/enum_providers.dart'; import 'package:lightmeter/screens/settings/components/shared/settings_section/widget_settings_section.dart'; import 'package:lightmeter/screens/settings/components/theme/components/dynamic_color/widget_list_tile_dynamic_color.dart'; import 'package:lightmeter/screens/settings/components/theme/components/primary_color/widget_list_tile_primary_color.dart'; import 'package:lightmeter/screens/settings/components/theme/components/theme_type/widget_list_tile_theme_type.dart'; -import 'package:lightmeter/utils/inherited_generics.dart'; class ThemeSettingsSection extends StatelessWidget { const ThemeSettingsSection({super.key}); @@ -17,7 +17,7 @@ class ThemeSettingsSection extends StatelessWidget { children: [ const ThemeTypeListTile(), const PrimaryColorListTile(), - if (context.get() != DynamicColorState.unavailable) + if (EnumProviders.dynamicColorStateOf(context) != DynamicColorState.unavailable) const DynamicColorListTile(), ], ); diff --git a/test/data/shared_prefs_service_test.dart b/test/data/shared_prefs_service_test.dart index 514629f..692cb81 100644 --- a/test/data/shared_prefs_service_test.dart +++ b/test/data/shared_prefs_service_test.dart @@ -6,7 +6,7 @@ import 'package:lightmeter/data/models/metering_screen_layout_config.dart'; import 'package:lightmeter/data/models/supported_locale.dart'; import 'package:lightmeter/data/models/theme_type.dart'; import 'package:lightmeter/data/shared_prefs_service.dart'; -import 'package:lightmeter/providers/theme_provider.dart'; +import 'package:lightmeter/res/theme.dart'; import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; import 'package:mocktail/mocktail.dart'; import 'package:shared_preferences/shared_preferences.dart'; @@ -348,13 +348,13 @@ void main() { group('primaryColor', () { test('get default', () { when(() => sharedPreferences.getInt(UserPreferencesService.primaryColorKey)).thenReturn(null); - expect(service.primaryColor, ThemeProvider.primaryColorsList[5]); + expect(service.primaryColor, primaryColorsList[5]); }); test('get', () { when(() => sharedPreferences.getInt(UserPreferencesService.primaryColorKey)) .thenReturn(0xff9c27b0); - expect(service.primaryColor, ThemeProvider.primaryColorsList[2]); + expect(service.primaryColor, primaryColorsList[2]); }); test('set', () {