栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用SearchDelegate在flutter中显示最近的搜索历史?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用SearchDelegate在flutter中显示最近的搜索历史?

custom_search_delgates.dart

import 'package:flutter/material.dart';typedef onSearchChanged = Future<List<String>> Function(String);class SearchWithSuggestionDelegate extends SearchDelegate<String> {  ///[onSearchChanged] gets the [query] as an argument. Then this callback  ///should process [query] then return an [List<String>] as suggestions.  ///Since its returns a [Future] you get suggestions from server too.  final onSearchChanged onSearchChanged;  ///This [_oldFilters] used to store the previous suggestions. While waiting  ///for [onSearchChanged] to completed, [_oldFilters] are displayed.  List<String> _oldFilters = const [];  SearchWithSuggestionDelegate({String searchFieldLabel, this.onSearchChanged})      : super(searchFieldLabel: searchFieldLabel);  ///  @override  Widget buildLeading(BuildContext context) {    return IconButton(      icon: const Icon(Icons.arrow_back),      onPressed: () => Navigator.pop(context),    );  }  @override  List<Widget> buildActions(BuildContext context) {    return [      IconButton(        icon: Icon(Icons.clear),        onPressed: () => query = "",      ),    ];  }  ///onSubmit in the keyboard, returns the [query]  @override  void showResults(BuildContext context) {    close(context, query);  }  ///Since [showResults] is overridden we can don't have to build the results.  @override  Widget buildResults(BuildContext context) => null;  @override  Widget buildSuggestions(BuildContext context) {    return FutureBuilder<List<String>>(      future: onSearchChanged != null ? onSearchChanged(query) : null,      builder: (context, snapshot) {        if (snapshot.hasData) _oldFilters = snapshot.data;        return ListView.builder(          itemCount: _oldFilters.length,          itemBuilder: (context, index) { return ListTile(   leading: Icon(Icons.restore),   title: Text("${_oldFilters[index]}"),   onTap: () => close(context, _oldFilters[index]), );          },        );      },    );  }}

用法:

import 'package:flutter/material.dart';import 'package:flutter_app/custom_search_delgates.dart';import 'package:shared_preferences/shared_preferences.dart';void main() {  runApp(MaterialApp(home: Home()));}class Home extends StatefulWidget {  @override  _HomeState createState() => _HomeState();}class _HomeState extends State<Home> {  Future<void> _showSearch() async {    final searchText = await showSearch<String>(      context: context,      delegate: SearchWithSuggestionDelegate(        onSearchChanged: _getRecentSearchesLike,      ),    );    //Save the searchText to SharedPref so that next time you can use them as recent searches.    await _saveToRecentSearches(searchText);    //Do something with searchText. Note: This is not a result.  }  Future<List<String>> _getRecentSearchesLike(String query) async {    final pref = await SharedPreferences.getInstance();    final allSearches = pref.getStringList("recentSearches");    return allSearches.where((search) => search.startsWith(query)).toList();  }  Future<void> _saveToRecentSearches(String searchText) async {    if (searchText == null) return; //Should not be null    final pref = await SharedPreferences.getInstance();    //Use `Set` to avoid duplication of recentSearches    Set<String> allSearches =        pref.getStringList("recentSearches")?.toSet() ?? {};    //Place it at first in the set    allSearches = {searchText, ...allSearches};    pref.setStringList("recentSearches", allSearches.toList());  }  @override  Widget build(BuildContext context) {    return Scaffold(      appBar: AppBar(        title: Text("Search Demo"),        actions: <Widget>[          IconButton( icon: Icon(Icons.search), onPressed: _showSearch,          ),        ],      ),    );  }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/387581.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号