我需要让Google使用默认的FLUTER的SearchPage来放置搜索建议,每当用户开始键入时,我都需要给出自动完成的建议,而我使用FutureBuilder
异步地实现了这一点,现在的问题是,我需要在500ms或更长时间内取消搜索请求的分派,而不是在用户仍在键入时浪费大量请求
总结一下我到目前为止所做的工作:
1) 在我的小部件中,我调用
showSearch(context: context, delegate: _delegate);
2)我的代理如下所示:
class _LocationSearchDelegate extends SearchDelegate<Suggestion> {
@override
List<Widget> buildActions(BuildContext context) {
return <Widget>[
IconButton(
tooltip: 'Clear',
icon: const Icon(Icons.clear),
onPressed: () {
query = '';
showSuggestions(context);
},
)
];
}
@override
Widget buildLeading(BuildContext context) => IconButton(
tooltip: 'Back',
icon: AnimatedIcon(
icon: AnimatedIcons.menu_arrow,
progress: transitionAnimation,
),
onPressed: () {
close(context, null);
},
);
@override
Widget buildResults(BuildContext context) {
return FutureBuilder<List<Suggestion>>(
future: GooglePlaces.getInstance().getAutocompleteSuggestions(query),
builder: (BuildContext context, AsyncSnapshot<List<Suggestion>> suggestions) {
if (!suggestions.hasData) {
return Text('No results');
}
return buildLocationSuggestions(suggestions.data);
},
);
}
@override
Widget buildSuggestions(BuildContext context) {
return buildResults(context);
}
Widget buildLocationSuggestions(List<Suggestion> suggestions) {
return ListView.builder(
itemBuilder: (context, index) => ListTile(
leading: Icon(Icons.location_on),
title: Text(suggestions[index].text),
onTap: () {
showResults(context);
},
),
itemCount: suggestions.length,
);
}
}
3-我需要限制/取消搜索,直到xxx毫秒过go
我有一个 idea ,有没有一种简单的方法可以将FutureBuilder转换为Stream并使用Stream Builder(我在一些文章中看到它支持debounce )?
**我知道有一些像TypeAhead
这样的第三方自动完成小工具正在(从头开始)这样做,但我现在不想使用这个.