我正在编写一个应用程序,其中我使用futurebuilder返回一个单独的dart文件中的小部件,该文件返回一个Listview Builder。快照中的数据通过其构造函数传递给窗口小部件。
我有一个搜索功能,当输入搜索查询时,它仅使用新过滤的列表来调用小部件。它(几乎)有效,但是我知道有解决此问题的更好方法。
在这种情况下是否可以实现有状态的小部件?如果不是,是否有可能将状态传递给称为此listview类的有状态类?
我要重新加载小部件的部分在buildResults函数中。
Widget listViewWidget(
BuildContext context, List<Car> itemsFromFutureBuilder, CarDataProvider carProvider) {
items = itemsFromFutureBuilder;
carProviderus = carProvider;
carNamesForSearching = items.map((x) => x.model).toList();
return Scaffold(
body: new Container(
color: Color.fromRGBO(119, 119, 119, 1),
child: Stack(children: <Widget>[
Container(
height: 400.0,
width: MediaQuery.of(context).size.width,
),
ListView.builder( //---------------------------------------------------List Builder
itemCount: items.length,
itemBuilder: (context, index) {
return Dismissible(
key: Key(items[index].id.toString()),
...
)
// ------------------------------SEARCH--------------------------------------------
class DataSearch extends SearchDelegate<String> {
@override
List<Widget> buildActions(BuildContext context) {
return [
IconButton(
icon: Icon(Icons.clear),
onPressed: () {
query = "";
})
];
}
@override
Widget buildLeading(BuildContext context) {
return IconButton(
icon: AnimatedIcon(
icon: AnimatedIcons.menu_arrow,
progress: transitionAnimation,
),
onPressed: () {
close(context, null);
},
);
}
@override
Widget buildResults(BuildContext context) {
//var newItems = await carProviderus.returnCarsThatStartWith(query);
String queryus = query;
return listViewWidget(context, items.where((x) => x.model.toUpperCase().startsWith(queryus.toUpperCase())).toList(), null);
}
@override
Widget buildSuggestions(BuildContext context) {
List<String> suggestionList = query.isEmpty
? carNamesForSearching
: carNamesForSearching
.where((x) => x.toUpperCase().startsWith(query.toUpperCase()))
.toList();
return ListView.builder(
itemBuilder: (context, index) => ListTile(
onTap: () {
query = suggestionList[index];
showResults(context);
},
leading: Icon(Icons.local_car_wash),
title: RichText(
text: TextSpan(
text: suggestionList[index].substring(0, query.length),
style:
TextStyle(color: Colors.blueAccent, fontWeight: FontWeight.bold),
children: [
TextSpan(
text: suggestionList[index].substring(query.length),
style: TextStyle(color: Colors.black))
],
)),
),
itemCount: suggestionList.length,
);
}
}```