I want to show a list of titles from my search but when I type any of the titles no listTile is displayed as well as the ProgressIndicator
.The Provider.of<NotesProvider>(context, listen: false)
.usernotes is a list of titles. Any idea with respect to the question will be accepted.
class _SearchState extends State<Search> {
@override
Widget build(BuildContext context) {
var searchprovider =
Provider.of<NotesProvider>(context, listen: false).usernotes;
var itemlist = [];
var filteritems = [];
bool isSearching = false;
void descriptionlist({required String items}) async {
setState(() {
isSearching = true;
searchprovider.forEach((element) {
itemlist.add(element.description);
});
filteritems = itemlist
.where((element) => element.contains(items.toLowerCase()))
.toList();
print(filteritems);
});
isSearching = false;
}
return Scaffold(
appBar: AppBar(
title: Container(
width: 150,
height: 50,
child: TextField(
onChanged: (value) {
descriptionlist(items: value);
},
)),
),
body: isSearching
? CircularProgressIndicator()
: ListView.builder(
itemCount: filteritems.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(
filteritems[index],
style: TextStyle(fontSize: 50),
));
}),
);
}
}
The print values of filteritems
shows that the titles I wanted to display exist
2
Answers
You need to call
setState
after filtering the items so that the widget rebuilds with the updatedfilteritems
. Additionally, you should wrap the CircularProgressIndicator widget inside a Visibility widget and set its visible property toisSearching
. This way, the CircularProgressIndicator will only be visible while the search is in progress.A TextEditingController named _searchController is introduced to handle the search text field’s text changes and provide access to the entered query.
The itemlist is populated in the initState() method, where it extracts the description property from each element of the usernotes list using map() and toList().
The searchItems() function is modified to receive the query string and perform the filtering operation. The search is case-insensitive, and the filtered results are stored in filteritems.
The TextField inside the app bar now has a hint text provided using the decoration property of InputDecoration.
A CircularProgressIndicator is displayed when isSearching is true to indicate that the search operation is in progress.
The ListView.builder is updated to use a smaller font size (fontSize: 20) for the filtered items.