diff --git a/lib/providers/equipment_profile_provider.dart b/lib/providers/equipment_profile_provider.dart index d57019c..a11c63b 100644 --- a/lib/providers/equipment_profile_provider.dart +++ b/lib/providers/equipment_profile_provider.dart @@ -77,8 +77,12 @@ class EquipmentProfiles extends InheritedWidget { super.key, }); - static List? of(BuildContext context) { - return context.dependOnInheritedWidgetOfExactType()?.profiles; + static List? of(BuildContext context, {bool listen = true}) { + if (listen) { + return context.dependOnInheritedWidgetOfExactType()?.profiles; + } else { + return context.findAncestorWidgetOfExactType()?.profiles; + } } @override diff --git a/lib/screens/settings/components/metering/components/equipment_profiles/components/equipment_profile_screen/components/equipment_profile_section/components/equipment_list_tiles/widget_list_tiles_equipments.dart b/lib/screens/settings/components/metering/components/equipment_profiles/components/equipment_profile_screen/components/equipment_profile_container/components/equipment_list_tiles/widget_list_tiles_equipments.dart similarity index 100% rename from lib/screens/settings/components/metering/components/equipment_profiles/components/equipment_profile_screen/components/equipment_profile_section/components/equipment_list_tiles/widget_list_tiles_equipments.dart rename to lib/screens/settings/components/metering/components/equipment_profiles/components/equipment_profile_screen/components/equipment_profile_container/components/equipment_list_tiles/widget_list_tiles_equipments.dart diff --git a/lib/screens/settings/components/metering/components/equipment_profiles/components/equipment_profile_screen/components/equipment_profile_section/widget_section_equipment_profile.dart b/lib/screens/settings/components/metering/components/equipment_profiles/components/equipment_profile_screen/components/equipment_profile_container/widget_container_equipment_profile.dart similarity index 79% rename from lib/screens/settings/components/metering/components/equipment_profiles/components/equipment_profile_screen/components/equipment_profile_section/widget_section_equipment_profile.dart rename to lib/screens/settings/components/metering/components/equipment_profiles/components/equipment_profile_screen/components/equipment_profile_container/widget_container_equipment_profile.dart index 5adf931..d3e8dd0 100644 --- a/lib/screens/settings/components/metering/components/equipment_profiles/components/equipment_profile_screen/components/equipment_profile_section/widget_section_equipment_profile.dart +++ b/lib/screens/settings/components/metering/components/equipment_profiles/components/equipment_profile_screen/components/equipment_profile_container/widget_container_equipment_profile.dart @@ -4,21 +4,23 @@ import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; import 'components/equipment_list_tiles/widget_list_tiles_equipments.dart'; -class EquipmentListTilesSection extends StatefulWidget { +class EquipmentProfileContainer extends StatefulWidget { final EquipmentProfileData data; + final VoidCallback onExpand; final VoidCallback onDelete; - const EquipmentListTilesSection({ + const EquipmentProfileContainer({ required this.data, + required this.onExpand, required this.onDelete, super.key, }); @override - State createState() => _EquipmentListTilesSectionState(); + State createState() => EquipmentProfileContainerState(); } -class _EquipmentListTilesSectionState extends State { +class EquipmentProfileContainerState extends State { final TextEditingController _nameController = TextEditingController(text: 'Default'); final FocusNode _fieldFocusNode = FocusNode(); bool _expanded = false; @@ -93,22 +95,28 @@ class _EquipmentListTilesSectionState extends State { Widget _collapseButton() { return IconButton( - onPressed: () { - setState(() { - _expanded = !_expanded; - }); - if (_expanded) { - SchedulerBinding.instance.addPostFrameCallback((_) { - Scrollable.ensureVisible( - context, - alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtEnd, - ); - }); - } else { - _fieldFocusNode.unfocus(); - } - }, + onPressed: _expanded ? collapse : expand, icon: Icon(_expanded ? Icons.keyboard_arrow_up : Icons.keyboard_arrow_down), ); } + + void expand() { + widget.onExpand(); + setState(() { + _expanded = true; + }); + SchedulerBinding.instance.addPostFrameCallback((_) { + Scrollable.ensureVisible( + context, + alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtEnd, + ); + }); + } + + void collapse() { + setState(() { + _expanded = false; + }); + _fieldFocusNode.unfocus(); + } } diff --git a/lib/screens/settings/components/metering/components/equipment_profiles/components/equipment_profile_screen/screen_equipment_profile.dart b/lib/screens/settings/components/metering/components/equipment_profiles/components/equipment_profile_screen/screen_equipment_profile.dart index 2e32f3a..cedc5f6 100644 --- a/lib/screens/settings/components/metering/components/equipment_profiles/components/equipment_profile_screen/screen_equipment_profile.dart +++ b/lib/screens/settings/components/metering/components/equipment_profiles/components/equipment_profile_screen/screen_equipment_profile.dart @@ -3,7 +3,7 @@ import 'package:lightmeter/generated/l10n.dart'; import 'package:lightmeter/providers/equipment_profile_provider.dart'; import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; -import 'components/equipment_profile_section/widget_section_equipment_profile.dart'; +import 'components/equipment_profile_container/widget_container_equipment_profile.dart'; class EquipmentProfileScreen extends StatefulWidget { const EquipmentProfileScreen({super.key}); @@ -13,6 +13,22 @@ class EquipmentProfileScreen extends StatefulWidget { } class _EquipmentProfileScreenState extends State { + static const maxProfiles = 5; // replace with a constant from iap + + late List> profileContainersKeys = []; + + int get profilesCount => EquipmentProfiles.of(context)?.length ?? 0; + + @override + void initState() { + super.initState(); + profileContainersKeys = List.filled( + EquipmentProfiles.of(context, listen: false)?.length ?? 0, + GlobalKey(), + growable: true, + ); + } + @override Widget build(BuildContext context) { return Scaffold( @@ -34,21 +50,38 @@ class _EquipmentProfileScreenState extends State { kFloatingActionButtonMargin, ), separatorBuilder: (context, index) => const SizedBox(height: Dimens.grid16), - itemCount: EquipmentProfiles.of(context)?.length ?? 0, - itemBuilder: (_, index) => EquipmentListTilesSection( + itemCount: profilesCount, + itemBuilder: (context, index) => EquipmentProfileContainer( + key: profileContainersKeys[index], data: EquipmentProfiles.of(context)![index], + onExpand: () => _keepExpandedAt(index), onDelete: () { EquipmentProfileProvider.of(context) .deleteProfile(EquipmentProfiles.of(context)![index]); + profileContainersKeys.removeAt(index); }, ), ), ), floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat, - floatingActionButton: FloatingActionButton( - onPressed: EquipmentProfileProvider.of(context).addProfile, - child: const Icon(Icons.add), - ), + floatingActionButton: profilesCount < maxProfiles + ? FloatingActionButton( + onPressed: () { + EquipmentProfileProvider.of(context).addProfile(); + profileContainersKeys.add(GlobalKey()); + }, + child: const Icon(Icons.add), + ) + : null, ); } + + void _keepExpandedAt(int index) { + profileContainersKeys.getRange(0, index).forEach((element) { + element.currentState?.collapse(); + }); + profileContainersKeys.getRange(index + 1, profilesCount).forEach((element) { + element.currentState?.collapse(); + }); + } }