removed Provider from providers folder

This commit is contained in:
Vadim 2023-05-18 19:56:13 +02:00
parent 2c85a3fddc
commit 3bb2b7845b
15 changed files with 99 additions and 137 deletions

View file

@ -19,6 +19,7 @@ import 'package:lightmeter/providers/supported_locale_provider.dart';
import 'package:lightmeter/providers/theme_provider.dart';
import 'package:lightmeter/screens/metering/flow_metering.dart';
import 'package:lightmeter/screens/settings/flow_settings.dart';
import 'package:lightmeter/utils/inherited_generics.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
@ -53,26 +54,28 @@ class Application extends StatelessWidget {
child: EvSourceTypeProvider(
child: SupportedLocaleProvider(
child: ThemeProvider(
builder: (context, _) => _AnnotatedRegionWrapper(
child: MaterialApp(
theme: context.watch<ThemeData>(),
locale: Locale(context.watch<SupportedLocale>().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!,
child: Builder(
builder: (context) => _AnnotatedRegionWrapper(
child: MaterialApp(
theme: context.listen<ThemeData>(),
locale: Locale(context.listen<SupportedLocale>().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(),
},
),
initialRoute: "metering",
routes: {
"metering": (context) => const MeteringFlow(),
"settings": (context) => const SettingsFlow(),
},
),
),
),
@ -100,7 +103,7 @@ class _AnnotatedRegionWrapper extends StatelessWidget {
@override
Widget build(BuildContext context) {
final systemIconsBrightness = ThemeData.estimateBrightnessForColor(
context.watch<ThemeData>().colorScheme.onSurface,
context.listen<ThemeData>().colorScheme.onSurface,
);
return AnnotatedRegion(
value: SystemUiOverlayStyle(

View file

@ -1,9 +1,13 @@
import 'package:flutter/material.dart';
import 'package:lightmeter/data/shared_prefs_service.dart';
import 'package:lightmeter/utils/inherited_generics.dart';
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
import 'package:provider/provider.dart';
import 'package:uuid/uuid.dart';
typedef EquipmentProfiles = List<EquipmentProfileData>;
typedef EquipmentProfile = EquipmentProfileData;
class EquipmentProfileProvider extends StatefulWidget {
final Widget child;
@ -47,9 +51,9 @@ class EquipmentProfileProviderState extends State<EquipmentProfileProvider> {
@override
Widget build(BuildContext context) {
return EquipmentProfiles(
profiles: [_defaultProfile] + _customProfiles,
child: EquipmentProfile(
return InheritedWidgetBase<List<EquipmentProfileData>>(
data: [_defaultProfile] + _customProfiles,
child: InheritedWidgetBase<EquipmentProfileData>(
data: _selectedProfile,
child: widget.child,
),
@ -96,45 +100,3 @@ class EquipmentProfileProviderState extends State<EquipmentProfileProvider> {
setState(() {});
}
}
class EquipmentProfiles extends InheritedWidget {
final List<EquipmentProfileData> profiles;
const EquipmentProfiles({
required this.profiles,
required super.child,
super.key,
});
static List<EquipmentProfileData> of(BuildContext context, {bool listen = true}) {
if (listen) {
return context.dependOnInheritedWidgetOfExactType<EquipmentProfiles>()!.profiles;
} else {
return context.findAncestorWidgetOfExactType<EquipmentProfiles>()!.profiles;
}
}
@override
bool updateShouldNotify(EquipmentProfiles oldWidget) => true;
}
class EquipmentProfile extends InheritedWidget {
final EquipmentProfileData data;
const EquipmentProfile({
required this.data,
required super.child,
super.key,
});
static EquipmentProfileData of(BuildContext context, {bool listen = true}) {
if (listen) {
return context.dependOnInheritedWidgetOfExactType<EquipmentProfile>()!.data;
} else {
return context.findAncestorWidgetOfExactType<EquipmentProfile>()!.data;
}
}
@override
bool updateShouldNotify(EquipmentProfile oldWidget) => true;
}

View file

@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:lightmeter/data/models/ev_source_type.dart';
import 'package:lightmeter/data/shared_prefs_service.dart';
import 'package:lightmeter/environment.dart';
import 'package:lightmeter/utils/inherited_generics.dart';
import 'package:provider/provider.dart';
class EvSourceTypeProvider extends StatefulWidget {
@ -41,9 +42,9 @@ class EvSourceTypeProviderState extends State<EvSourceTypeProvider> {
Widget build(BuildContext context) {
return ValueListenableBuilder(
valueListenable: valueListenable,
builder: (_, value, child) => Provider.value(
value: value,
child: child,
builder: (_, value, child) => InheritedWidgetBase<EvSourceType>(
data: value,
child: child!,
),
child: widget.child,
);

View file

@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:lightmeter/data/models/supported_locale.dart';
import 'package:lightmeter/data/shared_prefs_service.dart';
import 'package:lightmeter/generated/l10n.dart';
import 'package:lightmeter/utils/inherited_generics.dart';
import 'package:provider/provider.dart';
class SupportedLocaleProvider extends StatefulWidget {
@ -36,9 +37,9 @@ class SupportedLocaleProviderState extends State<SupportedLocaleProvider> {
Widget build(BuildContext context) {
return ValueListenableBuilder(
valueListenable: valueListenable,
builder: (_, value, child) => Provider.value(
value: value,
child: child,
builder: (_, value, child) => InheritedWidgetBase<SupportedLocale>(
data: value,
child: child!,
),
child: widget.child,
);

View file

@ -5,15 +5,16 @@ 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/res/dimens.dart';
import 'package:lightmeter/utils/inherited_generics.dart';
import 'package:material_color_utilities/material_color_utilities.dart';
import 'package:provider/provider.dart';
class ThemeProvider extends StatefulWidget {
final TransitionBuilder? builder;
final Widget child;
const ThemeProvider({
this.builder,
required this.child,
super.key,
});
@ -76,8 +77,8 @@ class ThemeProviderState extends State<ThemeProvider> with WidgetsBindingObserve
Widget build(BuildContext context) {
return ValueListenableBuilder(
valueListenable: _themeTypeNotifier,
builder: (_, themeType, __) => Provider.value(
value: themeType,
builder: (_, themeType, __) => InheritedWidgetBase<ThemeType>(
data: themeType,
child: ValueListenableBuilder(
valueListenable: _dynamicColorNotifier,
builder: (_, useDynamicColor, __) => _DynamicColorProvider(
@ -88,7 +89,7 @@ class ThemeProviderState extends State<ThemeProvider> with WidgetsBindingObserve
builder: (_, primaryColor, __) => _ThemeDataProvider(
primaryColor: dynamicPrimaryColor ?? primaryColor,
brightness: _themeBrightness,
builder: widget.builder,
child: widget.child,
),
),
),
@ -154,8 +155,8 @@ class _DynamicColorProvider extends StatelessWidget {
dynamicPrimaryColor = null;
state = DynamicColorState.unavailable;
}
return Provider.value(
value: state,
return InheritedWidgetBase<DynamicColorState>(
data: state,
child: builder(context, dynamicPrimaryColor),
);
},
@ -166,19 +167,19 @@ class _DynamicColorProvider extends StatelessWidget {
class _ThemeDataProvider extends StatelessWidget {
final Color primaryColor;
final Brightness brightness;
final TransitionBuilder? builder;
final Widget child;
const _ThemeDataProvider({
required this.primaryColor,
required this.brightness,
required this.builder,
required this.child,
});
@override
Widget build(BuildContext context) {
return Provider.value(
value: _themeFromColorScheme(_colorSchemeFromColor()),
builder: builder,
return InheritedWidgetBase<ThemeData>(
data: _themeFromColorScheme(_colorSchemeFromColor()),
child: child,
);
}

View file

@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'package:lightmeter/data/models/ev_source_type.dart';
import 'package:lightmeter/res/dimens.dart';
import 'package:lightmeter/screens/metering/components/bottom_controls/components/measure_button/widget_button_measure.dart';
import 'package:provider/provider.dart';
import 'package:lightmeter/utils/inherited_generics.dart';
class MeteringBottomControls extends StatelessWidget {
final double? ev;
@ -44,7 +44,7 @@ class MeteringBottomControls extends StatelessWidget {
child: IconButton(
onPressed: onSwitchEvSourceType,
icon: Icon(
context.watch<EvSourceType>() != EvSourceType.camera
context.listen<EvSourceType>() != EvSourceType.camera
? Icons.camera_rear
: Icons.wb_incandescent,
),

View file

@ -9,6 +9,7 @@ import 'package:lightmeter/providers/metering_screen_layout_provider.dart';
import 'package:lightmeter/res/dimens.dart';
import 'package:lightmeter/screens/metering/components/shared/readings_container/components/animated_dialog_picker/widget_picker_dialog_animated.dart';
import 'package:lightmeter/screens/metering/components/shared/readings_container/components/reading_value_container/widget_container_reading_value.dart';
import 'package:lightmeter/utils/inherited_generics.dart';
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
class ReadingsContainer extends StatelessWidget {
@ -76,7 +77,7 @@ class ReadingsContainer extends StatelessWidget {
Expanded(
child: _IsoValuePicker(
selectedValue: iso,
values: EquipmentProfile.of(context).isoValues,
values: context.listen<EquipmentProfile>().isoValues,
onChanged: onIsoChanged,
),
),
@ -84,7 +85,7 @@ class ReadingsContainer extends StatelessWidget {
Expanded(
child: _NdValuePicker(
selectedValue: nd,
values: EquipmentProfile.of(context).ndValues,
values: context.listen<EquipmentProfile>().ndValues,
onChanged: onNdChanged,
),
),
@ -107,16 +108,16 @@ class _EquipmentProfilePicker extends StatelessWidget {
return AnimatedDialogPicker<EquipmentProfileData>(
icon: Icons.camera,
title: S.of(context).equipmentProfile,
selectedValue: EquipmentProfile.of(context),
values: EquipmentProfiles.of(context),
selectedValue: context.listen<EquipmentProfile>(),
values: context.listen<EquipmentProfiles>(),
itemTitleBuilder: (_, value) => Text(value.id.isEmpty ? S.of(context).none : value.name),
onChanged: EquipmentProfileProvider.of(context).setProfile,
closedChild: ReadingValueContainer.singleValue(
value: ReadingValue(
label: S.of(context).equipmentProfile,
value: EquipmentProfile.of(context).id.isEmpty
value: context.listen<EquipmentProfile>().id.isEmpty
? S.of(context).none
: EquipmentProfile.of(context).name,
: context.listen<EquipmentProfile>().name,
),
),
);

View file

@ -39,7 +39,7 @@ class _MeteringFlowState extends State<MeteringFlow> {
create: (context) => MeteringBloc(
context.read<MeteringCommunicationBloc>(),
context.read<MeteringInteractor>(),
EquipmentProfile.of(context, listen: false),
context.get<EquipmentProfile>(),
context.get<StopType>(),
),
),

View file

@ -16,22 +16,9 @@ import 'package:lightmeter/screens/metering/state_metering.dart';
import 'package:lightmeter/utils/inherited_generics.dart';
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
class MeteringScreen extends StatefulWidget {
class MeteringScreen extends StatelessWidget {
const MeteringScreen({super.key});
@override
State<MeteringScreen> createState() => _MeteringScreenState();
}
class _MeteringScreenState extends State<MeteringScreen> {
MeteringBloc get _bloc => context.read<MeteringBloc>();
@override
void didChangeDependencies() {
super.didChangeDependencies();
_bloc.add(EquipmentProfileChangedEvent(EquipmentProfile.of(context)));
}
@override
Widget build(BuildContext context) {
return _InheritedListeners(
@ -48,22 +35,23 @@ class _MeteringScreenState extends State<MeteringScreen> {
film: state.film,
iso: state.iso,
nd: state.nd,
onFilmChanged: (value) => _bloc.add(FilmChangedEvent(value)),
onIsoChanged: (value) => _bloc.add(IsoChangedEvent(value)),
onNdChanged: (value) => _bloc.add(NdChangedEvent(value)),
onFilmChanged: (value) =>
context.read<MeteringBloc>().add(FilmChangedEvent(value)),
onIsoChanged: (value) => context.read<MeteringBloc>().add(IsoChangedEvent(value)),
onNdChanged: (value) => context.read<MeteringBloc>().add(NdChangedEvent(value)),
),
),
),
BlocBuilder<MeteringBloc, MeteringState>(
builder: (context, state) => MeteringBottomControlsProvider(
ev: state is MeteringDataState ? state.ev : null,
isMetering: state is LoadingState ||
state is MeteringDataState && state.continuousMetering,
isMetering:
state is LoadingState || state is MeteringDataState && state.continuousMetering,
hasError: state is MeteringDataState && state.hasError,
onSwitchEvSourceType: context.read<Environment>().hasLightSensor
? EvSourceTypeProvider.of(context).toggleType
: null,
onMeasure: () => _bloc.add(const MeasureEvent()),
onMeasure: () => context.read<MeteringBloc>().add(const MeasureEvent()),
onSettings: () => Navigator.pushNamed(context, 'settings'),
),
),
@ -81,16 +69,21 @@ class _InheritedListeners extends StatelessWidget {
@override
Widget build(BuildContext context) {
return InheritedWidgetListener<StopType>(
return InheritedWidgetListener<EquipmentProfile>(
onDidChangeDependencies: (value) {
context.read<MeteringBloc>().add(StopTypeChangedEvent(value));
context.read<MeteringBloc>().add(EquipmentProfileChangedEvent(value));
},
child: InheritedModelAspectListener<MeteringScreenLayoutFeature, bool>(
aspect: MeteringScreenLayoutFeature.filmPicker,
child: InheritedWidgetListener<StopType>(
onDidChangeDependencies: (value) {
if (!value) context.read<MeteringBloc>().add(const FilmChangedEvent(Film.other()));
context.read<MeteringBloc>().add(StopTypeChangedEvent(value));
},
child: child,
child: InheritedModelAspectListener<MeteringScreenLayoutFeature, bool>(
aspect: MeteringScreenLayoutFeature.filmPicker,
onDidChangeDependencies: (value) {
if (!value) context.read<MeteringBloc>().add(const FilmChangedEvent(Film.other()));
},
child: child,
),
),
);
}
@ -121,7 +114,7 @@ class _MeteringContainerBuidler extends StatelessWidget {
@override
Widget build(BuildContext context) {
return context.watch<EvSourceType>() == EvSourceType.camera
return context.listen<EvSourceType>() == EvSourceType.camera
? CameraContainerProvider(
fastest: fastest,
slowest: slowest,

View file

@ -3,7 +3,7 @@ import 'package:lightmeter/data/models/supported_locale.dart';
import 'package:lightmeter/generated/l10n.dart';
import 'package:lightmeter/providers/supported_locale_provider.dart';
import 'package:lightmeter/screens/settings/components/shared/dialog_picker.dart/widget_dialog_picker.dart';
import 'package:provider/provider.dart';
import 'package:lightmeter/utils/inherited_generics.dart';
class LanguageListTile extends StatelessWidget {
const LanguageListTile({super.key});
@ -13,14 +13,14 @@ class LanguageListTile extends StatelessWidget {
return ListTile(
leading: const Icon(Icons.language),
title: Text(S.of(context).language),
trailing: Text(context.watch<SupportedLocale>().localizedName),
trailing: Text(context.listen<SupportedLocale>().localizedName),
onTap: () {
showDialog<SupportedLocale>(
context: context,
builder: (_) => DialogPicker<SupportedLocale>(
icon: Icons.language,
title: S.of(context).chooseLanguage,
selectedValue: context.read<SupportedLocale>(),
selectedValue: context.get<SupportedLocale>(),
values: SupportedLocale.values,
titleAdapter: (context, value) => value.localizedName,
),

View file

@ -5,6 +5,7 @@ import 'package:lightmeter/res/dimens.dart';
import 'package:lightmeter/screens/settings/components/metering/components/equipment_profiles/components/equipment_profile_screen/components/equipment_profile_container/widget_container_equipment_profile.dart';
import 'package:lightmeter/screens/settings/components/metering/components/equipment_profiles/components/equipment_profile_screen/components/equipment_profile_name_dialog/widget_dialog_equipment_profile_name.dart';
import 'package:lightmeter/screens/shared/sliver_screen/screen_sliver.dart';
import 'package:lightmeter/utils/inherited_generics.dart';
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
class EquipmentProfilesScreen extends StatefulWidget {
@ -18,12 +19,13 @@ class _EquipmentProfilesScreenState extends State<EquipmentProfilesScreen> {
static const maxProfiles = 5 + 1; // replace with a constant from iap
late List<GlobalKey<EquipmentProfileContainerState>> profileContainersKeys = [];
int get profilesCount => EquipmentProfiles.of(context).length;
int get profilesCount => context.listen<EquipmentProfiles>().length;
@override
void initState() {
super.initState();
profileContainersKeys = EquipmentProfiles.of(context, listen: false)
profileContainersKeys = context
.get<EquipmentProfiles>()
.map((e) => GlobalKey<EquipmentProfileContainerState>(debugLabel: e.id))
.toList();
}
@ -56,7 +58,7 @@ class _EquipmentProfilesScreenState extends State<EquipmentProfilesScreen> {
),
child: EquipmentProfileContainer(
key: profileContainersKeys[index],
data: EquipmentProfiles.of(context)[index],
data: context.listen<EquipmentProfiles>()[index],
onExpand: () => _keepExpandedAt(index),
onUpdate: (profileData) => _updateProfileAt(profileData, index),
onDelete: () => _removeProfileAt(index),
@ -87,7 +89,7 @@ class _EquipmentProfilesScreenState extends State<EquipmentProfilesScreen> {
}
void _removeProfileAt(int index) {
EquipmentProfileProvider.of(context).deleteProfile(EquipmentProfiles.of(context)[index]);
EquipmentProfileProvider.of(context).deleteProfile(context.listen<EquipmentProfiles>()[index]);
profileContainersKeys.removeAt(index);
}

View file

@ -1,8 +1,8 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.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/utils/inherited_generics.dart';
class DynamicColorListTile extends StatelessWidget {
const DynamicColorListTile({super.key});
@ -12,7 +12,7 @@ class DynamicColorListTile extends StatelessWidget {
return SwitchListTile(
secondary: const Icon(Icons.colorize),
title: Text(S.of(context).dynamicColor),
value: context.watch<DynamicColorState>() == DynamicColorState.enabled,
value: context.listen<DynamicColorState>() == DynamicColorState.enabled,
onChanged: ThemeProvider.of(context).enableDynamicColor,
);
}

View file

@ -1,18 +1,17 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.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/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.watch<DynamicColorState>() == DynamicColorState.enabled) {
if (context.listen<DynamicColorState>() == DynamicColorState.enabled) {
return Opacity(
opacity: Dimens.disabledOpacity,
child: IgnorePointer(

View file

@ -3,7 +3,7 @@ import 'package:lightmeter/data/models/theme_type.dart';
import 'package:lightmeter/generated/l10n.dart';
import 'package:lightmeter/providers/theme_provider.dart';
import 'package:lightmeter/screens/settings/components/shared/dialog_picker.dart/widget_dialog_picker.dart';
import 'package:provider/provider.dart';
import 'package:lightmeter/utils/inherited_generics.dart';
class ThemeTypeListTile extends StatelessWidget {
const ThemeTypeListTile({super.key});
@ -13,14 +13,14 @@ class ThemeTypeListTile extends StatelessWidget {
return ListTile(
leading: const Icon(Icons.brightness_6),
title: Text(S.of(context).theme),
trailing: Text(_typeToString(context, context.watch<ThemeType>())),
trailing: Text(_typeToString(context, context.listen<ThemeType>())),
onTap: () {
showDialog<ThemeType>(
context: context,
builder: (_) => DialogPicker<ThemeType>(
icon: Icons.brightness_6,
title: S.of(context).chooseTheme,
selectedValue: context.read<ThemeType>(),
selectedValue: context.get<ThemeType>(),
values: ThemeType.values,
titleAdapter: _typeToString,
),

View file

@ -1,12 +1,11 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:lightmeter/data/models/dynamic_colors_state.dart';
import 'package:lightmeter/generated/l10n.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});
@ -18,7 +17,7 @@ class ThemeSettingsSection extends StatelessWidget {
children: [
const ThemeTypeListTile(),
const PrimaryColorListTile(),
if (context.read<DynamicColorState>() != DynamicColorState.unavailable)
if (context.get<DynamicColorState>() != DynamicColorState.unavailable)
const DynamicColorListTile(),
],
);