flutter - Listview搜索在列表中找不到数据
问题描述
我正在尝试为 SW Api 中的字符添加名称搜索,但由于某种原因搜索不起作用,有人可以告诉我错误在哪里吗?也许问题出在列表本身?
class _SWMainState extends State<SWMain> {
Icon customIcon = Icon(Icons.search);
static Text titleText = Text("Star Wars API");
Widget customSearchBar = titleText;
var search = List<Character>();
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: customSearchBar,
actions: <Widget>[
IconButton(
icon: customIcon,
onPressed: () {
setState(() {
if (this.customIcon.icon == Icons.search) {
this.customIcon = Icon(Icons.cancel);
this.customSearchBar = TextField(
textInputAction: TextInputAction.go,
decoration: InputDecoration(
border: InputBorder.none,
hintText: "Search character"),
onChanged: (text) {
text = text.toLowerCase();
search = search.where((search) {
var charTitle = search.name.toLowerCase();
return charTitle.contains(text);
}).toList();
},
style: TextStyle(color: Colors.white, fontSize: 16),
);
} else {
this.customIcon = Icon(Icons.search);
this.customSearchBar = titleText;
}
});
})
],
),
),
);
}
}
解决方案
首先,flutter 中的每个搜索实现都需要一个 Search Delegate 来负责渲染 Search Results Widget。
在这种情况下,您可以像这样创建一个委托类:
class CustomSearchItemsDelegate extends SearchDelegate {
@override
List<Widget> buildActions(BuildContext context) {
return [
IconButton(
icon: Icon(Icons.clear),
onPressed: () {
query = 'My Query coming from Variable';
},
),
];
}
@override
Widget buildLeading(BuildContext context) {
return IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {
close(context, null);
},
);
}
@override
Widget buildResults(BuildContext context) {
if (query.length == 0) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Center(
child: Text(
"Please enter an appropriate search field...",
),
)
],
);
}
return Column(
children: <Widget>[
//Render the Search Results Widget here if any exist
],
);
}
@override
Widget buildSuggestions(BuildContext context) {
return Column();
}
}
然后在您负责调用搜索的类 Widget 中,添加以下内容:
await showSearch(
query: "My Search Query...", context: context, delegate: CustomSearchItemsDelegate());
所以你最终状态的组件类将是:
class _SWMainState extends State<SWMain> {
Icon customIcon = Icon(Icons.search);
static Text titleText = Text("Star Wars API");
Widget customSearchBar = titleText;
var search = List<Character>();
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: customSearchBar,
actions: <Widget>[
IconButton(
icon: customIcon,
onPressed: () async {
await showSearch(query: "My Search Query...", context:context, delegate: CustomSearchItemsDelegate()); //Let the search delegate manage the UI
});
})
],
),
),
);
}
}
然后就让CustomSearchItemsDelegate管理 UI 和它自己的状态管理
推荐阅读
- performance - 有没有更好的方法在 MATLAB 中为类属性赋值?
- python - 在创建视图类中使用模型的先前数据存储在其他模型中
- angular - 当凭据在一端工作但在另一端不工作时如何修复 401 错误
- c - FFT 返回 NaN 值
- python - 如何在python中创建最小堆来存储边缘和重量
- javascript - React 基于 Web 的应用程序连接到物理硬件设备
- reactjs - 如何在 React 中创建水平时间线
- python - 从数组中删除 n 个连续元素,使剩余元素的幅度最小
- go - 将证书添加到 Postman 以连接到 Go 服务器
- flutter - 根据 Flutter 中应用的主题更改 AppIcon 的图像