implemented MeteringScreenLayoutProvider

This commit is contained in:
Vadim 2023-04-03 19:02:18 +03:00
parent be0617a99c
commit 12d44d6bab
2 changed files with 102 additions and 24 deletions

View file

@ -18,6 +18,7 @@ import 'environment.dart';
import 'generated/l10n.dart'; import 'generated/l10n.dart';
import 'providers/equipment_profile_provider.dart'; import 'providers/equipment_profile_provider.dart';
import 'providers/ev_source_type_provider.dart'; import 'providers/ev_source_type_provider.dart';
import 'providers/features_provider.dart';
import 'providers/theme_provider.dart'; import 'providers/theme_provider.dart';
import 'screens/metering/flow_metering.dart'; import 'screens/metering/flow_metering.dart';
import 'screens/settings/flow_settings.dart'; import 'screens/settings/flow_settings.dart';
@ -47,6 +48,7 @@ class Application extends StatelessWidget {
Provider(create: (_) => PermissionsService()), Provider(create: (_) => PermissionsService()),
Provider(create: (_) => const LightSensorService()), Provider(create: (_) => const LightSensorService()),
], ],
child: MeteringScreenLayoutProvider(
child: StopTypeProvider( child: StopTypeProvider(
child: EquipmentProfileProvider( child: EquipmentProfileProvider(
child: EvSourceTypeProvider( child: EvSourceTypeProvider(
@ -79,6 +81,7 @@ class Application extends StatelessWidget {
), ),
), ),
), ),
),
); );
} }
return const SizedBox(); return const SizedBox();

View file

@ -0,0 +1,75 @@
import 'package:flutter/material.dart';
enum MeteringScreenLayoutFeature { extremeExposurePairs, reciprocity }
class MeteringScreenLayoutProvider extends StatefulWidget {
final Widget child;
const MeteringScreenLayoutProvider({required this.child, super.key});
static MeteringScreenLayoutProviderState of(BuildContext context) {
return context.findAncestorStateOfType<MeteringScreenLayoutProviderState>()!;
}
@override
State<MeteringScreenLayoutProvider> createState() => MeteringScreenLayoutProviderState();
}
class MeteringScreenLayoutProviderState extends State<MeteringScreenLayoutProvider> {
final Map<MeteringScreenLayoutFeature, bool> _features = {
MeteringScreenLayoutFeature.extremeExposurePairs: true,
MeteringScreenLayoutFeature.reciprocity: true,
};
@override
Widget build(BuildContext context) {
return MeteringScreenLayout(
features: _features,
child: widget.child,
);
}
void setFeature(MeteringScreenLayoutFeature feature, {required bool enabled}) {
setState(() {
_features.update(
feature,
(_) => enabled,
ifAbsent: () => enabled,
);
});
}
}
class MeteringScreenLayout extends InheritedModel<MeteringScreenLayoutFeature> {
final Map<MeteringScreenLayoutFeature, bool> features;
const MeteringScreenLayout({
required this.features,
required super.child,
super.key,
});
static bool of(BuildContext context, MeteringScreenLayoutFeature feature, {bool listen = true}) {
if (listen) {
return context.dependOnInheritedWidgetOfExactType<MeteringScreenLayout>()!.features[feature]!;
} else {
return context.findAncestorWidgetOfExactType<MeteringScreenLayout>()!.features[feature]!;
}
}
@override
bool updateShouldNotify(MeteringScreenLayout oldWidget) => true;
@override
bool updateShouldNotifyDependent(
MeteringScreenLayout oldWidget,
Set<MeteringScreenLayoutFeature> dependencies,
) {
for (final dependecy in dependencies) {
if (oldWidget.features[dependecy] != features[dependecy]) {
return true;
}
}
return false;
}
}