Flutter SearchDelegate 带搜索历史记录

class searchBarDelegate extends SearchDelegate {
  @override
  void showResults(BuildContext context) {
    super.showResults(context);
    if (query == "") {
      return;
    }
    const sufix = "asdasdas56";
    var data = [];
    var history = SpUtil.getString("histry");
    if (history == null) {
      history = "";
    }
    data = history.split(sufix);

    while (data.length >= 10) {
      data.removeAt(0);
    }
    if (!data.contains(query)) {
      data.add(query);
      SpUtil.putString("histry", data.join(sufix).toString());
    }

    SpUtil.putString("search", query);
    Navigator.of(context).pop(query);
  }

  // 搜索条右侧的按钮执行方法,我们在这里方法里放入一个clear图标。 当点击图片时,清空搜索的内容。
  @override
  List<Widget> buildActions(BuildContext context) {
    return [
      IconButton(
        color: Colors.blue,
        icon: Icon(Icons.clear),
        onPressed: () => {
          if (query == "") {close(context, null)} else {query = ""}
        },
      )
    ];
  }

  // 搜索栏左侧的图标和功能,点击时关闭整个搜索页面
  @override
  Widget buildLeading(BuildContext context) {
    return IconButton(
      color: Colors.blue,
      icon: AnimatedIcon(
          icon: AnimatedIcons.menu_arrow, progress: transitionAnimation),
      onPressed: () => close(context, null),
    );
  }

  // 搜索到内容了
  @override
  Widget buildResults(BuildContext context) {
    //缓存下来记录
    return Center(
      child: Container(
        child: Card(
          color: Colors.redAccent,
          child: Text(query),
        ),
      ),
    );
  }

  // 输入时的推荐及搜索结果
  @override
  Widget buildSuggestions(BuildContext context) {
    const sufix = "asdasdas56";
    var history = SpUtil.getString("histry");
    if (history == null) {
      history = "";
    }
    var searchHistory = history
        .split(sufix)
        .reversed
        .where((element) => element != '')
        .toList();

    final suggestionList = query.isEmpty
        ? searchHistory
        : [].where((input) => input.startsWith(query)).toList();
    return ListView.builder(
      itemCount: suggestionList.length == 0 ? 0 : suggestionList.length,
      itemBuilder: (context, index) => ListTile(
        title: RichText(
          text: TextSpan(
              text: suggestionList[index].substring(0, query.length),
              style:
                  TextStyle(color: Colors.black, fontWeight: FontWeight.bold),
              children: [
                TextSpan(
                    text: suggestionList[index].substring(query.length),
                    style: TextStyle(color: Colors.grey))
              ]),
        ),
      ),
    );
    ;
  }
}

使用到了第三方

  sp_util: ^2.0.3

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安果移不动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值