美文网首页Flutter
Flutter实现搜索框中文占位提示文字

Flutter实现搜索框中文占位提示文字

作者: IT晴天 | 来源:发表于2019-12-27 09:59 被阅读0次

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: '搜索')
      );

相关文章

网友评论

    本文标题:Flutter实现搜索框中文占位提示文字

    本文链接:https://www.haomeiwen.com/subject/ghbroctx.html