buildTextField method

TextField buildTextField()

lib/demo/search_textfield_bar.dart 构建搜索输入TextField

Implementation

TextField buildTextField() {
  return TextField(
    ///设置键盘的类型
    keyboardType: TextInputType.text,

    ///键盘回车键的样式为搜索
    textInputAction: TextInputAction.search,

    ///只有苹果手机上有效果
    keyboardAppearance: Brightness.dark,

    ///控制器配置
    controller: widget.controller == null ? defaultController : widget.controller,

    ///最大行数
    maxLines: 1,

    ///输入文本格式过滤
    inputFormatters: [
      ///输入的内容长度限制
      LengthLimitingTextInputFormatter(widget.inputKeyWordsLength),
    ],

    ///当输入文本时实时回调
    onChanged: (text) {
      ///多层判断 优化刷新
      ///只有当有改变时再刷新
      if (text.length > 0) {
        if (!showClear) {
          setState(() {
            showClear = true;
          });
        }
      } else {
        if (showClear) {
          setState(() {
            showClear = false;
          });
        }
      }

      if (widget.onChanged != null) {
        widget.onChanged!(text);
      }
    },

    ///点击键盘上的回车键
    ///或者是搜索按钮的回调
    onSubmitted: (text) {
      if (widget.onSubmitted != null) {
        widget.onSubmitted!(text);
      }
    },

    ///输入框不自动获取焦点
    autofocus: false,
    focusNode: widget.focusNode,

    style: TextStyle(fontSize: widget.fontSize, color: Colors.black54, fontWeight: FontWeight.w300),

    ///输入框的边框装饰
    decoration: InputDecoration(
        //重要 用于编辑框对齐
        isDense: true,

        ///清除文本内边跑
        contentPadding: EdgeInsets.zero,

        ///不设置边框
        border: InputBorder.none,

        ///设置提示文本
        hintText: widget.hint,

        ///设置提示文本的样式
        hintStyle: TextStyle(
          fontSize: widget.fontSize,
          color: Color(0xff999999),
        )),
  );
}