我有一个list view
和一个Appbar
。 Appbar
上有一个搜索图标,我称之为SearchDelegate
。以下是我的SearchDelegate
class StudentSearch extends SearchDelegate<StudentModel> {
final Observable<StudentModel> studentModelDataList;
StudentSearch(this.studentModelDataList);
@override
List<Widget> buildActions(BuildContext context) {
return [
IconButton(
icon: Icon(Icons.clear),
onPressed: () {
query = "";
}),
];
}
@override
Widget buildLeading(BuildContext context) {
return IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {
close(context, null);
});
}
@override
Widget buildResults(BuildContext context) {
return Text(query);
}
@override
Widget buildSuggestions(BuildContext context) {
return StreamBuilder(
stream: studentModelDataList,
builder: (context, AsyncSnapshot<StudentModel> snapshot) {
if (!snapshot.hasData || snapshot.data == null) {
return Center(
child: CircularProgressIndicator(),
);
}
final results = snapshot.data.studentModelData.studentData
.where((a) => a.studName.toLowerCase().contains(query.toLowerCase()))
.toList();
if (snapshot.data != null) {
return Refresh(
year_id: "2",
schoolId: "1",
lastIndex: "0",
disciplineId: "1",
child: ListView.builder(
itemBuilder: (context, int index) {
return ListTile(
onTap: (){
print(snapshot.data.studentModelData.studentData[index].studName);
query = snapshot.data.studentModelData.studentData[index].studName;
close(context, snapshot.data);
},
title: Text(results[index].studName),
subtitle: Text('${results[index].studentEmail} '),
trailing: Column(
children: [
Icon(Icons.comment),
Text('${results[index].classCode}'),
],
),
);
},
itemCount: results.length,
),
);
}
});
}
}
我从文档中了解到,每当我输入要搜索的项目时,SearchDelegate
首先显示buildSuggestions
,然后显示buildResults
。因此,我尝试按学生姓名进行搜索,并且buildSuggestions方法可以正常工作,但是当我单击buildSuggestions项时,它不会向我显示buildresults
答案 0 :(得分:0)
BuildSuggestions 就是为了做到这一点,针对搜索栏中键入的内容构建建议列表。侦听器对“文本字段”状态的每个更改做出响应。这可以用来向用户提供自动完成建议。
BuildResults 是在用户通过单击键盘上的“放大镜”按钮来调用结果时调用的,或者用于“提交”结果以表明用户已完成其结果的任何内容输入并要求输入文本搜索结果。
您可以在这里Flutter's Search Support (The Boring Flutter Development Show, Ep. 10)
查看说明从documentation中获取buildResults(BuildContext上下文)→小部件“用户从搜索页面提交搜索后显示的结果。”
答案 1 :(得分:0)
尝试在ListTile小部件中的ontap()会话上调用buildResults函数。还可以像这样通过内部的BuildResults调用要显示的数据
@override
Widget buildResults(BuildContext context,["Data you want displayed when you click
search"]) {
return Text(query);
}
在这样的建议区域
onTap: () =>
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Scaffold(
appBar: AppBar(
backgroundColor:
Colors.blueGrey[900],
title: new Text(
"Country",
style: TextStyle(
color: Colors.white),
)),
body: buildResults(context)),
)),
当然,这仅是一个示例,但是您希望将buildResults称为“在您的法庭上”。这就是它的工作方式,在这种情况下,无论是在“ BuildResults(BuildContext,context)”中,buildResult(context)都会从“最终结果”中获取您正在使用的数据,并像这样在调用或使用的代码中显示>
final results = snapshot.data.length;
results = filteredUsers
.where((a) => (a.region.contains(query))
|| a.region.contains(query)
)
.toList();
return ListView.builder(
itemCount: results.length,
itemBuilder: (context, index) => Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
//As used in-code
Text(${results[index].region})
]))
因此,在我的案例中,“您希望在点击时显示的数据 搜索” = [结果]表示为
@override
Widget buildResults(BuildContext context, [results]) {
//How you want your BuildResult page to look like
}