From 4f387fe6ebeb90da2a4657b41fd704d60c9129cf Mon Sep 17 00:00:00 2001 From: Nils-Johan Gynther Date: Fri, 1 May 2026 02:41:02 +0200 Subject: [PATCH] feat: enhance category selection flow by adding preselection support and a select button --- .../core/ui/category_then_product_picker.dart | 44 ++++++++++++++----- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/flutter/lib/core/ui/category_then_product_picker.dart b/flutter/lib/core/ui/category_then_product_picker.dart index 17faefa0..ee80d443 100644 --- a/flutter/lib/core/ui/category_then_product_picker.dart +++ b/flutter/lib/core/ui/category_then_product_picker.dart @@ -49,18 +49,28 @@ class CategoryThenProductPicker { int? preselectedCategoryId, Future Function(String name, int categoryId)? onCreate, }) async { - // Steg 1 — välj kategori - final selectedCategory = await showModalBottomSheet( - context: context, - isScrollControlled: true, - useSafeArea: true, - builder: (ctx) => _CategoryPickerSheet( - tree: categoryTree, - preselectedId: preselectedCategoryId, - onSelected: (node) => Navigator.pop(ctx, node), - ), - ); - if (selectedCategory == null || !context.mounted) return null; + AdminCategoryNode? selectedCategory; + + if (preselectedCategoryId != null) { + // AI-föreslagen kategori — hoppa direkt till produktpickern + selectedCategory = _findNode(categoryTree, preselectedCategoryId); + } + + if (selectedCategory == null) { + // Steg 1 — visa kategoriträd + if (!context.mounted) return null; + selectedCategory = await showModalBottomSheet( + context: context, + isScrollControlled: true, + useSafeArea: true, + builder: (ctx) => _CategoryPickerSheet( + tree: categoryTree, + preselectedId: preselectedCategoryId, + onSelected: (node) => Navigator.pop(ctx, node), + ), + ); + if (selectedCategory == null || !context.mounted) return null; + } // Samla alla kategori-IDs i den valda grenen (inkl. ättlingar) final categoryIds = _collectIds(selectedCategory); @@ -319,6 +329,16 @@ class _CategoryTileState extends State<_CategoryTile> { fontWeight: FontWeight.w600, ), ), + trailing: TextButton( + style: TextButton.styleFrom( + padding: const EdgeInsets.symmetric(horizontal: 8), + minimumSize: const Size(0, 32), + tapTargetSize: MaterialTapTargetSize.shrinkWrap, + visualDensity: VisualDensity.compact, + ), + onPressed: () => widget.onLeafTap(node), + child: const Text('Välj'), + ), onTap: () => setState(() => _expanded = !_expanded), ), if (_expanded)