feat: add initial query support to ProductPickerField and enhance ParsedReceiptItem with categorySuggestionPath
This commit is contained in:
@@ -33,6 +33,9 @@ class ProductPickerField extends StatelessWidget {
|
||||
/// Inline error message shown below the field.
|
||||
final String? errorText;
|
||||
|
||||
/// If set, the picker bottom sheet opens with this text pre-filled in the search field.
|
||||
final String? initialQuery;
|
||||
|
||||
const ProductPickerField({
|
||||
super.key,
|
||||
required this.products,
|
||||
@@ -42,6 +45,7 @@ class ProductPickerField extends StatelessWidget {
|
||||
this.isLoading = false,
|
||||
this.label = 'Produkt',
|
||||
this.errorText,
|
||||
this.initialQuery,
|
||||
});
|
||||
|
||||
@override
|
||||
@@ -87,26 +91,49 @@ class ProductPickerField extends StatelessWidget {
|
||||
}
|
||||
|
||||
Future<void> _openPicker(BuildContext context) async {
|
||||
final result = await ProductPickerField.showSheet(
|
||||
context,
|
||||
products: products,
|
||||
value: value,
|
||||
label: label,
|
||||
initialQuery: initialQuery,
|
||||
);
|
||||
if (!context.mounted) return;
|
||||
if (result == null) return;
|
||||
if (result == _clearSelectionToken) {
|
||||
onChanged?.call(null);
|
||||
return;
|
||||
}
|
||||
if (result is int) onChanged?.call(result);
|
||||
}
|
||||
|
||||
/// Öppnar produktväljarens bottenark utan att binda den till en specifik widget-instans.
|
||||
/// Returnerar valt produkt-id, null (ingen ändring), eller [_clearSelectionToken] (rensa).
|
||||
static Future<int?> showSheet(
|
||||
BuildContext context, {
|
||||
required List<ProductOption> products,
|
||||
int? value,
|
||||
String label = 'Produkt',
|
||||
String? initialQuery,
|
||||
}) async {
|
||||
final result = await showModalBottomSheet<Object?>(
|
||||
context: context,
|
||||
isScrollControlled: true,
|
||||
useSafeArea: true,
|
||||
builder: (sheetContext) {
|
||||
var query = '';
|
||||
var query = initialQuery ?? '';
|
||||
|
||||
return StatefulBuilder(
|
||||
builder: (context, setModalState) {
|
||||
builder: (ctx, setModalState) {
|
||||
final normalizedQuery = query.trim().toLowerCase();
|
||||
final filtered = normalizedQuery.isEmpty
|
||||
? products
|
||||
: products
|
||||
.where(
|
||||
(p) => p.name.toLowerCase().contains(normalizedQuery),
|
||||
)
|
||||
.where((p) => p.name.toLowerCase().contains(normalizedQuery))
|
||||
.toList();
|
||||
|
||||
return SizedBox(
|
||||
height: MediaQuery.of(context).size.height * 0.85,
|
||||
height: MediaQuery.of(ctx).size.height * 0.85,
|
||||
child: Column(
|
||||
children: [
|
||||
Padding(
|
||||
@@ -114,14 +141,10 @@ class ProductPickerField extends StatelessWidget {
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
label,
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
child: Text(label, style: Theme.of(ctx).textTheme.titleMedium),
|
||||
),
|
||||
TextButton.icon(
|
||||
onPressed: () =>
|
||||
Navigator.pop(context, _clearSelectionToken),
|
||||
onPressed: () => Navigator.pop(ctx, _clearSelectionToken),
|
||||
icon: const Icon(Icons.clear),
|
||||
label: const Text('Rensa'),
|
||||
),
|
||||
@@ -132,37 +155,30 @@ class ProductPickerField extends StatelessWidget {
|
||||
padding: const EdgeInsets.fromLTRB(16, 0, 16, 8),
|
||||
child: TextField(
|
||||
autofocus: true,
|
||||
controller: TextEditingController(text: initialQuery ?? ''),
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'Sök produkt...',
|
||||
prefixIcon: Icon(Icons.search),
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
onChanged: (text) {
|
||||
setModalState(() {
|
||||
query = text;
|
||||
});
|
||||
},
|
||||
onChanged: (text) => setModalState(() => query = text),
|
||||
),
|
||||
),
|
||||
const Divider(height: 1),
|
||||
Expanded(
|
||||
child: filtered.isEmpty
|
||||
? const Center(
|
||||
child: Text('Inga produkter matchar sökningen.'),
|
||||
)
|
||||
? const Center(child: Text('Inga produkter matchar sökningen.'))
|
||||
: ListView.separated(
|
||||
itemCount: filtered.length,
|
||||
separatorBuilder: (_, __) => const Divider(height: 1),
|
||||
itemBuilder: (context, index) {
|
||||
itemBuilder: (ctx2, index) {
|
||||
final product = filtered[index];
|
||||
final isSelected = product.id == value;
|
||||
return ListTile(
|
||||
selected: isSelected,
|
||||
title: Text(product.name),
|
||||
trailing: isSelected
|
||||
? const Icon(Icons.check)
|
||||
: null,
|
||||
onTap: () => Navigator.pop(context, product.id),
|
||||
trailing: isSelected ? const Icon(Icons.check) : null,
|
||||
onTap: () => Navigator.pop(ctx2, product.id),
|
||||
);
|
||||
},
|
||||
),
|
||||
@@ -175,16 +191,8 @@ class ProductPickerField extends StatelessWidget {
|
||||
},
|
||||
);
|
||||
|
||||
if (!context.mounted) return;
|
||||
if (result == null) {
|
||||
return;
|
||||
}
|
||||
if (result == _clearSelectionToken) {
|
||||
onChanged?.call(null);
|
||||
return;
|
||||
}
|
||||
if (result is int) {
|
||||
onChanged?.call(result);
|
||||
}
|
||||
if (result == null || result == _clearSelectionToken) return null;
|
||||
if (result is int) return result;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user