2023-03-17 11:47:58 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
|
2023-03-20 21:03:19 +00:00
|
|
|
import 'package:uuid/uuid.dart';
|
2023-03-17 11:47:58 +00:00
|
|
|
|
|
|
|
class EquipmentProfileProvider extends StatefulWidget {
|
|
|
|
final Widget child;
|
|
|
|
|
|
|
|
const EquipmentProfileProvider({required this.child, super.key});
|
|
|
|
|
|
|
|
static EquipmentProfileProviderState of(BuildContext context) {
|
|
|
|
return context.findAncestorStateOfType<EquipmentProfileProviderState>()!;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<EquipmentProfileProvider> createState() => EquipmentProfileProviderState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class EquipmentProfileProviderState extends State<EquipmentProfileProvider> {
|
2023-03-23 05:05:28 +00:00
|
|
|
final List<EquipmentProfileData> _profiles = [
|
|
|
|
const EquipmentProfileData(
|
|
|
|
id: '',
|
|
|
|
name: '',
|
|
|
|
apertureValues: apertureValues,
|
|
|
|
ndValues: ndValues,
|
|
|
|
shutterSpeedValues: shutterSpeedValues,
|
|
|
|
isoValues: isoValues,
|
|
|
|
)
|
|
|
|
];
|
2023-03-17 18:24:07 +00:00
|
|
|
|
2023-03-23 05:05:28 +00:00
|
|
|
late EquipmentProfileData _selectedProfile = _profiles.first;
|
2023-03-17 11:47:58 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return EquipmentProfiles(
|
|
|
|
profiles: _profiles,
|
|
|
|
child: EquipmentProfile(
|
|
|
|
data: _selectedProfile,
|
|
|
|
child: widget.child,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setProfile(EquipmentProfileData data) {
|
|
|
|
setState(() {
|
|
|
|
_selectedProfile = data;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-20 19:39:07 +00:00
|
|
|
/// Creates a default equipment profile
|
2023-03-20 20:12:28 +00:00
|
|
|
void addProfile(String name) {
|
|
|
|
_profiles.add(EquipmentProfileData(
|
2023-03-20 21:03:19 +00:00
|
|
|
id: const Uuid().v1(),
|
2023-03-20 20:12:28 +00:00
|
|
|
name: name,
|
2023-03-20 19:39:07 +00:00
|
|
|
apertureValues: apertureValues,
|
|
|
|
ndValues: ndValues,
|
|
|
|
shutterSpeedValues: shutterSpeedValues,
|
|
|
|
isoValues: isoValues,
|
|
|
|
));
|
|
|
|
setState(() {});
|
|
|
|
}
|
2023-03-17 11:47:58 +00:00
|
|
|
|
2023-03-20 19:39:07 +00:00
|
|
|
void updateProdile(EquipmentProfileData data) {
|
2023-03-20 21:03:19 +00:00
|
|
|
final indexToUpdate = _profiles.indexWhere((element) => element.id == data.id);
|
|
|
|
if (indexToUpdate >= 0) {
|
|
|
|
_profiles[indexToUpdate] = data;
|
|
|
|
setState(() {});
|
|
|
|
}
|
2023-03-20 19:39:07 +00:00
|
|
|
}
|
2023-03-17 11:47:58 +00:00
|
|
|
|
2023-03-20 19:39:07 +00:00
|
|
|
void deleteProfile(EquipmentProfileData data) {
|
|
|
|
_profiles.remove(data);
|
|
|
|
setState(() {});
|
|
|
|
}
|
2023-03-17 11:47:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class EquipmentProfiles extends InheritedWidget {
|
|
|
|
final List<EquipmentProfileData> profiles;
|
|
|
|
|
|
|
|
const EquipmentProfiles({
|
|
|
|
required this.profiles,
|
|
|
|
required super.child,
|
|
|
|
super.key,
|
|
|
|
});
|
|
|
|
|
2023-03-20 20:02:20 +00:00
|
|
|
static List<EquipmentProfileData>? of(BuildContext context, {bool listen = true}) {
|
|
|
|
if (listen) {
|
|
|
|
return context.dependOnInheritedWidgetOfExactType<EquipmentProfiles>()?.profiles;
|
|
|
|
} else {
|
|
|
|
return context.findAncestorWidgetOfExactType<EquipmentProfiles>()?.profiles;
|
|
|
|
}
|
2023-03-17 11:47:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool updateShouldNotify(EquipmentProfiles oldWidget) => true;
|
|
|
|
}
|
|
|
|
|
|
|
|
class EquipmentProfile extends InheritedWidget {
|
|
|
|
final EquipmentProfileData? data;
|
|
|
|
|
|
|
|
const EquipmentProfile({
|
|
|
|
required this.data,
|
|
|
|
required super.child,
|
|
|
|
super.key,
|
|
|
|
});
|
|
|
|
|
2023-03-17 18:24:07 +00:00
|
|
|
static EquipmentProfileData? of(BuildContext context, {bool listen = true}) {
|
|
|
|
if (listen) {
|
|
|
|
return context.dependOnInheritedWidgetOfExactType<EquipmentProfile>()?.data;
|
|
|
|
} else {
|
|
|
|
return context.findAncestorWidgetOfExactType<EquipmentProfile>()?.data;
|
|
|
|
}
|
2023-03-17 11:47:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool updateShouldNotify(EquipmentProfile oldWidget) => true;
|
|
|
|
}
|