Search by character in flutter

Search on type in listview in flutter 

Declare 2 List one is new search result list and other is existing list.

List searchresult = [];
List<String> allBillList = List.empty();

initialte TextField for search in container and assign controller

late TabController _tabController;

initialize controller inside init

@override
void initState() {
_tabController = new TabController(length: 4, vsync: this);
super.initState();
}
Container(
child: Visibility(
visible: isSearching,
child: Container(
height: 60.0,
padding: const EdgeInsets.all(8.0),
child: TextField(
textAlign: TextAlign.left,
controller: _controller,
style: new TextStyle(),
decoration: new InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20.0),
),
prefixIcon: new Icon(Icons.search, color: Colors.black),
hintText: "Search...",
hintStyle: new TextStyle(color: Colors.black)),
onChanged: searchOperation,
),
)));

Add SearchOperation method of onChanged

void searchOperation(String searchText) {
searchresult.clear();
if (isSearching != null) {
for (int i = 0; i < allBillList!.length; i++) {
String data = allBillList[i]!;
if (data.toLowerCase().contains(searchText.toLowerCase())) {
setState(() {
searchresult.add(allBillList![i]);
});
}
}
}
}

Initiate listview view with updated data

ListView.builder(
physics: ScrollPhysics(parent: AlwaysScrollableScrollPhysics()),
itemCount: searchresult!.length,
scrollDirection: Axis.vertical,
itemBuilder: (c, index1) {
String result = searchresult![index1];
return Container(
color: Colors.white,
child: Text(result);
);
}),

Comments

Popular posts from this blog

Flutter Bloc Pattern 8.0.0 Call API

Dynamic and responsive ListView in flutter

Manage Firebase storage using flutter