mirror of
https://github.com/Mercantec-GHC/h4-projekt-gruppe-0-sm.git
synced 2025-04-28 08:44:06 +02:00
search products
This commit is contained in:
parent
cea1a5f5bd
commit
b18e0455e7
@ -60,6 +60,7 @@ class AllProductsPage extends StatelessWidget {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
final productRepo = Provider.of<ProductRepo>(context);
|
||||||
return Row(
|
return Row(
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
children: [
|
children: [
|
||||||
@ -70,8 +71,11 @@ class AllProductsPage extends StatelessWidget {
|
|||||||
Expanded(
|
Expanded(
|
||||||
child: Container(
|
child: Container(
|
||||||
margin: const EdgeInsets.only(left: 10, right: 10),
|
margin: const EdgeInsets.only(left: 10, right: 10),
|
||||||
child: const TextField(
|
child: TextField(
|
||||||
decoration: InputDecoration(
|
onChanged: (query) {
|
||||||
|
productRepo.searchProducts(query);
|
||||||
|
},
|
||||||
|
decoration: const InputDecoration(
|
||||||
label: Text("Search"),
|
label: Text("Search"),
|
||||||
contentPadding: EdgeInsets.only(top: 20))),
|
contentPadding: EdgeInsets.only(top: 20))),
|
||||||
),
|
),
|
||||||
@ -80,7 +84,7 @@ class AllProductsPage extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
Expanded(
|
Expanded(
|
||||||
child: Consumer<ProductRepo>(builder: (_, productRepo, __) {
|
child: Consumer<ProductRepo>(builder: (_, productRepo, __) {
|
||||||
final products = productRepo.allProducts();
|
final products = productRepo.filteredProducts;
|
||||||
return ListView.builder(
|
return ListView.builder(
|
||||||
shrinkWrap: true,
|
shrinkWrap: true,
|
||||||
itemBuilder: (_, idx) => ProductListItem(
|
itemBuilder: (_, idx) => ProductListItem(
|
||||||
|
@ -3,8 +3,10 @@ import 'package:flutter/material.dart';
|
|||||||
class ProductRepo extends ChangeNotifier {
|
class ProductRepo extends ChangeNotifier {
|
||||||
int _nextId = 0;
|
int _nextId = 0;
|
||||||
List<Product> products = [];
|
List<Product> products = [];
|
||||||
|
late List<Product> filteredProducts;
|
||||||
ProductRepo() {
|
ProductRepo() {
|
||||||
_addAllProducts();
|
_addAllProducts();
|
||||||
|
filteredProducts = products;
|
||||||
}
|
}
|
||||||
|
|
||||||
int getNextId() {
|
int getNextId() {
|
||||||
@ -15,6 +17,22 @@ class ProductRepo extends ChangeNotifier {
|
|||||||
return products;
|
return products;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void searchProducts(String query) {
|
||||||
|
if (query.trim().isEmpty) {
|
||||||
|
filteredProducts = products;
|
||||||
|
} else {
|
||||||
|
filteredProducts = products.where((product) {
|
||||||
|
final nameLower = product.name.toLowerCase();
|
||||||
|
final descriptionLower = product.description.toLowerCase();
|
||||||
|
final searchLower = query.toLowerCase();
|
||||||
|
|
||||||
|
return nameLower.contains(searchLower) ||
|
||||||
|
descriptionLower.contains(searchLower);
|
||||||
|
}).toList();
|
||||||
|
}
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
|
||||||
void _addAllProducts() {
|
void _addAllProducts() {
|
||||||
products = [
|
products = [
|
||||||
Product(
|
Product(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user