feat: enhance ProductPickerField to use rawName for initial query in receipt import

This commit is contained in:
Nils-Johan Gynther
2026-05-01 02:00:13 +02:00
parent f4fea7b92c
commit 47801935e9
2 changed files with 7 additions and 3 deletions
@@ -91,6 +91,7 @@ class ProductPickerField extends StatelessWidget {
} }
Future<void> _openPicker(BuildContext context) async { Future<void> _openPicker(BuildContext context) async {
// Skapa controller utanför showSheet för att undvika reset vid rebuild
final result = await ProductPickerField.showSheet( final result = await ProductPickerField.showSheet(
context, context,
products: products, products: products,
@@ -121,6 +122,8 @@ class ProductPickerField extends StatelessWidget {
isScrollControlled: true, isScrollControlled: true,
useSafeArea: true, useSafeArea: true,
builder: (sheetContext) { builder: (sheetContext) {
// Skapa controller EN gång per öppnad ark — inte inuti StatefulBuilder
final controller = TextEditingController(text: initialQuery ?? '');
var query = initialQuery ?? ''; var query = initialQuery ?? '';
return StatefulBuilder( return StatefulBuilder(
@@ -155,7 +158,7 @@ class ProductPickerField extends StatelessWidget {
padding: const EdgeInsets.fromLTRB(16, 0, 16, 8), padding: const EdgeInsets.fromLTRB(16, 0, 16, 8),
child: TextField( child: TextField(
autofocus: true, autofocus: true,
controller: TextEditingController(text: initialQuery ?? ''), controller: controller,
decoration: const InputDecoration( decoration: const InputDecoration(
hintText: 'Sök produkt...', hintText: 'Sök produkt...',
prefixIcon: Icon(Icons.search), prefixIcon: Icon(Icons.search),
@@ -98,13 +98,14 @@ class _EditDialogState extends State<_EditDialog> {
_productName = item.suggestedProductName; _productName = item.suggestedProductName;
}); });
} else if (aiCategory != null) { } else if (aiCategory != null) {
// Öppna pickern med kategorinamnet sökt // Öppna pickern med råtexten från kvittot som sökord — kategorinamnet
// matchar inte produktnamn, men rawName gör det troligtvis
ProductPickerField.showSheet( ProductPickerField.showSheet(
context, context,
products: widget.products, products: widget.products,
value: _productId, value: _productId,
label: 'Produkt', label: 'Produkt',
initialQuery: aiCategory, initialQuery: item.rawName,
).then((id) { ).then((id) {
if (id != null && mounted) { if (id != null && mounted) {
setState(() { setState(() {