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,31 +48,33 @@ class Application extends StatelessWidget {
Provider(create: (_) => PermissionsService()), Provider(create: (_) => PermissionsService()),
Provider(create: (_) => const LightSensorService()), Provider(create: (_) => const LightSensorService()),
], ],
child: StopTypeProvider( child: MeteringScreenLayoutProvider(
child: EquipmentProfileProvider( child: StopTypeProvider(
child: EvSourceTypeProvider( child: EquipmentProfileProvider(
child: SupportedLocaleProvider( child: EvSourceTypeProvider(
child: ThemeProvider( child: SupportedLocaleProvider(
builder: (context, _) => _AnnotatedRegionWrapper( child: ThemeProvider(
child: MaterialApp( builder: (context, _) => _AnnotatedRegionWrapper(
theme: context.watch<ThemeData>(), child: MaterialApp(
locale: Locale(context.watch<SupportedLocale>().intlName), theme: context.watch<ThemeData>(),
localizationsDelegates: const [ locale: Locale(context.watch<SupportedLocale>().intlName),
S.delegate, localizationsDelegates: const [
GlobalMaterialLocalizations.delegate, S.delegate,
GlobalWidgetsLocalizations.delegate, GlobalMaterialLocalizations.delegate,
GlobalCupertinoLocalizations.delegate, GlobalWidgetsLocalizations.delegate,
], GlobalCupertinoLocalizations.delegate,
supportedLocales: S.delegate.supportedLocales, ],
builder: (context, child) => MediaQuery( supportedLocales: S.delegate.supportedLocales,
data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0), builder: (context, child) => MediaQuery(
child: child!, 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(),
},
), ),
), ),
), ),

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;
}
}