【问题标题】:SearchDelegate on query changeSearchDelegate 查询更改
【发布时间】: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 属性),但我找不到办法。 我能做什么?

【问题讨论】:

    标签: flutter flutter-appbar


    【解决方案1】:

    您可以将该方法拖放到 buildSuggestions Widget 中的任何位置;

    【讨论】:

    • 你能举个例子吗?
    猜你喜欢
    • 2019-06-28
    • 2020-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多