Refactor category handling in inventory and pantry forms
Test Suite / test (24.15.0) (push) Has been cancelled

- Introduced `_categorySearchController` and `_categoryOptions` in both `_InventoryFormDialogState` and `_PantryFormDialogState` to manage category selection more effectively.
- Implemented `_flattenCategoryOptions` method to create a flat list of category options from nested category nodes.
- Updated the initialization and disposal of the new controllers to ensure proper resource management.
- Made minor adjustments to the PopupMenuItem definitions in `AppShell` for consistency.
This commit is contained in:
Nils-Johan Gynther
2026-05-11 17:03:58 +02:00
parent d038d30831
commit 9b4d1f94bf
6 changed files with 84459 additions and 79916 deletions
@@ -506,6 +506,9 @@ class _PantryFormDialog extends StatefulWidget {
class _PantryFormDialogState extends State<_PantryFormDialog> {
late final TextEditingController _locationController;
late final TextEditingController _categorySearchController;
late List<CategorySelectOption> _categoryOptions;
int? _ownerUserId;
int? _productId;
int? _categoryId;
@@ -522,11 +525,14 @@ class _PantryFormDialogState extends State<_PantryFormDialog> {
_categoryId = initialProduct?.categoryId;
_categoryPath = initialProduct?.categoryPath;
_locationController = TextEditingController(text: initial?.location ?? '');
_categorySearchController = TextEditingController(text: _categoryPath ?? '');
_categoryOptions = _flattenCategoryOptions(widget.categories);
}
@override
void dispose() {
_locationController.dispose();
_categorySearchController.dispose();
super.dispose();
}
@@ -563,6 +569,20 @@ class _PantryFormDialogState extends State<_PantryFormDialog> {
.toList();
}
List<CategorySelectOption> _flattenCategoryOptions(
List<AdminCategoryNode> nodes, [
List<String> parents = const [],
]) {
final result = <CategorySelectOption>[];
for (final node in nodes) {
final pathParts = [...parents, node.name];
final path = pathParts.join(' > ');
result.add((value: node.id.toString(), label: path));
result.addAll(_flattenCategoryOptions(node.children, pathParts));
}
return result;
}
Future<void> _pickCategory() async {
final selected = await CategoryThenProductPicker.showCategorySheet(
context,