【发布时间】:2021-09-20 23:35:53
【问题描述】:
我创建了这个非常简单的类来扩展 SearchDelegate 以将其用作搜索栏:
class DataSearch extends SearchDelegate<String>{
@override
List<Widget>? buildActions(BuildContext context) {
return [
IconButton(
onPressed: () {query = '';},
icon: const Icon(Icons.clear))
];
}
@override
Widget? buildLeading(BuildContext context) {
return IconButton(
onPressed: (){close(context, '');},
icon: AnimatedIcon(
progress: transitionAnimation,
icon: AnimatedIcons.menu_arrow
));
}
@override
Widget buildResults(BuildContext context) {
//TODO show some results based on the selection
return Scaffold();
}
@override
Widget buildSuggestions(BuildContext context) {
final namesList = [];
final suggestionList = query.isEmpty ? [] : namesList;
return ListView.builder(
itemBuilder: (context, index) => ListTile(
title: RichText(
text: TextSpan(
text: suggestionList[index].substring(0, query.length),
style: const TextStyle(color: Colors.black, fontWeight: FontWeight.bold),
children: [
TextSpan(
text: suggestionList[index].substring(query.length),
style: const TextStyle(color: Colors.grey))
]),
),
),
itemCount: suggestionList.length,
);
}
}
它可以工作,但为了让它更好一点,我想每次用户在文本输入中插入/删除一个字母时对我的数据库进行一个新查询(类似于 TextField 的 onChanged 属性),但我找不到办法。 我能做什么?
【问题讨论】: