Flutter-Ernaehrungsapp/lib/pages/search_food/sub_components/search.dart

77 lines
2.3 KiB
Dart
Raw Normal View History

2023-05-29 12:08:46 +02:00
import 'package:empty_widget/empty_widget.dart';
import 'package:flutter/material.dart';
2023-06-25 14:43:07 +02:00
import '../../../models/food.dart';
import 'searched_food.dart';
2023-05-29 12:08:46 +02:00
class SearchComponent extends StatefulWidget {
final List<Food> foods;
final String cardName;
const SearchComponent(this.foods, this.cardName, {super.key});
@override
State<SearchComponent> createState() => _SearchComponentState();
}
class _SearchComponentState extends State<SearchComponent> {
final TextEditingController controller = TextEditingController();
late List<Food> allFoods = widget.foods;
List<Food> foundedFood = [];
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return Container(
constraints: const BoxConstraints.expand(),
child: Padding(
padding: const EdgeInsets.fromLTRB(16, 16, 16, 0), // Add padding on the X-axis
child: Column(
children: [
TextField(
controller: controller,
decoration: InputDecoration(
contentPadding: const EdgeInsets.symmetric(horizontal: 16), // Add padding on the X-axis for text input
prefix: const Icon(Icons.search),
hintText: 'Suche nach Gericht',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
borderSide: const BorderSide(color: Colors.blue),
2023-05-29 12:08:46 +02:00
),
),
onChanged: findOnChanged,
),
Expanded(
child: controller.text.isNotEmpty && foundedFood.isNotEmpty
? ListView.builder(
itemCount: foundedFood.length,
itemBuilder: (context, index) {
final food = foundedFood[index];
return SearchedFoodComponent(food, widget.cardName);
},
)
: EmptyWidget(),
),
],
),
),
);
2023-05-29 12:08:46 +02:00
}
void findOnChanged(String searchedFood) async{
foundedFood =
allFoods
.where((food) => food.name
.toString()
.toLowerCase()
.startsWith(searchedFood.toLowerCase()))
.toList();
setState(() {
foundedFood;
});
}
2023-06-25 14:43:07 +02:00
2023-05-29 12:08:46 +02:00
}