2023-04-05 19:15:11 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:lightmeter/data/models/metering_screen_layout_config.dart';
|
2023-08-13 18:58:11 +00:00
|
|
|
import 'package:lightmeter/providers/service_provider.dart';
|
2023-06-04 11:04:04 +00:00
|
|
|
import 'package:lightmeter/utils/inherited_generics.dart';
|
2023-04-05 19:15:11 +00:00
|
|
|
|
|
|
|
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> {
|
|
|
|
late final MeteringScreenLayoutConfig _config =
|
2023-08-13 18:58:11 +00:00
|
|
|
ServiceProvider.userPreferencesServiceOf(context).meteringScreenLayout;
|
2023-04-05 19:15:11 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-06-04 11:04:04 +00:00
|
|
|
return InheritedModelBase<MeteringScreenLayoutFeature, bool>(
|
|
|
|
data: MeteringScreenLayoutConfig.from(_config),
|
2023-04-05 19:15:11 +00:00
|
|
|
child: widget.child,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateFeatures(MeteringScreenLayoutConfig config) {
|
|
|
|
setState(() {
|
|
|
|
config.forEach((key, value) {
|
|
|
|
_config.update(
|
|
|
|
key,
|
|
|
|
(_) => value,
|
|
|
|
ifAbsent: () => value,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2023-08-13 18:58:11 +00:00
|
|
|
ServiceProvider.userPreferencesServiceOf(context).meteringScreenLayout = _config;
|
2023-04-05 19:15:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-04 11:04:04 +00:00
|
|
|
typedef _MeteringScreenLayoutModel = InheritedModelBase<MeteringScreenLayoutFeature, bool>;
|
2023-04-05 19:15:11 +00:00
|
|
|
|
2023-06-04 11:04:04 +00:00
|
|
|
extension MeteringScreenLayout on InheritedModelBase<MeteringScreenLayoutFeature, bool> {
|
2023-04-05 19:15:11 +00:00
|
|
|
static MeteringScreenLayoutConfig of(BuildContext context, {bool listen = true}) {
|
|
|
|
if (listen) {
|
2023-06-04 11:04:04 +00:00
|
|
|
return context.dependOnInheritedWidgetOfExactType<_MeteringScreenLayoutModel>()!.data;
|
2023-04-05 19:15:11 +00:00
|
|
|
} else {
|
2023-06-04 11:04:04 +00:00
|
|
|
return context.findAncestorWidgetOfExactType<_MeteringScreenLayoutModel>()!.data;
|
2023-04-05 19:15:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-04 11:04:04 +00:00
|
|
|
static bool featureOf(BuildContext context, MeteringScreenLayoutFeature aspect) {
|
|
|
|
return InheritedModel.inheritFrom<_MeteringScreenLayoutModel>(context, aspect: aspect)!
|
|
|
|
.data[aspect]!;
|
2023-04-05 19:15:11 +00:00
|
|
|
}
|
|
|
|
}
|