Flutter 自定义SearchDelegate搜索页面
2019-08-15 本文已影响0人
Karedem
日常开发中 我们可能会用到 SearchDelegate 来实现一个搜索页面
但是对外只暴露了以下几个接口
buildActions()
buildLeading()
buildResults()
buildSuggestion() 等等
如果我们想自定义 TextField , 修改hintText内容
或者AppBar下还想添加一个bottom怎么办呢, 这边提供一个方法以供参考
定义一个ASearchDelegate 继承自 SearchDelegate, 把SearchDelegate 源码都拷进来(material\search.dart)
showSearch方法也换个名 showASearch(内部类可以都加个A前缀以区分, 同时继承一下原有类)
接着 找到_ASearchPageState 的build方法
会返回一个Semantics 对象, 其实整个搜索页面就在这里定义的啦
ok, 就在这里自由发挥了
下图是题主的改动
![](https://img.haomeiwen.com/i3780893/3b25903bef6fdef1.png)
对应调用处则调整为: showASearch(context, ASearchDelegate());
效果图:
![](https://img.haomeiwen.com/i3780893/0e78881f07566246.png)