Flutter出来这么久了,网上居然很难搜到实现搜索框中文占位提示符(placeholder/hintText)的文章,难得找到的都是类似操作:
class SearchBarDelegate extends SearchDelegate<String>{
@override
String get searchFieldLabel => '搜索内容';
...
}
这样很不灵活,因为是get方法,组件不好自定义文字复用。
其实实现起来很简单,这样就好了:
class SearchBarDelegate extends SearchDelegate<String>{
SearchBarDelegate({
String hintText,
}) : super(
searchFieldLabel: hintText,
keyboardType: TextInputType.text,
textInputAction: TextInputAction.search,
);
...
}
然后如下使用:
showSearch(context: context,
delegate: SearchBarDelegate(hintText: '搜索')
);