场景:在历史搜索过程中,点击某个搜索热点词,自动将该词填入,并进行搜索。

如下gif图:

在这里插入图片描述

这时候我们自动填充教师资格证的内容,然后可以进行删除重新操作等,此时用的不再是hintText这个属性,而是用TextEditingController来进行控制,

具体代码如下:

  Widget searchBar() {
    TextEditingController controller = TextEditingController.fromValue(
        TextEditingValue(
            text: _searchContent,
            selection: new TextSelection.fromPosition(TextPosition(
                affinity: TextAffinity.downstream,
                offset: _searchContent.length))));

    return Container(
      child: Row(
        children: <Widget>[
          Expanded(
            child: Container(
              alignment: Alignment.center,
              child: Padding(
                padding: EdgeInsets.only(left: 10),
                child: TextField(
                  controller: controller,
                  decoration: InputDecoration(
                    hintText: "请输入想搜索的内容 $_searchContent",
                    border: InputBorder.none,
                  ),
                  onChanged: searchContentChange,
                  style:
                      TextStyle(color: MyColorRes.subTextColor, fontSize: 14),
                  onSubmitted: (content) {
                    if (content.isNotEmpty) {
                      print("content onSubmitted ===============    $content");
                      _searchContent = content;
                      getSearchContentResult();
                    }else {
                      ToastUtil.showBottomToast("请输入搜索内容");
                    }
                  },
                ),
              ),
              decoration: BoxDecoration(
                color: MyColorRes.divideLine,
                borderRadius: BorderRadius.all(Radius.circular(20)),
                border: Border.all(color: Colors.white, width: 1.0),
              ),
            ),
            flex: 1,
          ),
          Container(
            margin: const EdgeInsets.only(right: 10, left: 10),
            child: InkWell(
              onTap: () {
                Application.router.pop(context);
              },
              child: Text(
                "取消",
                style: TextStyle(color: Colors.black, fontSize: 16),
              ),
            ),
          )
        ],
      ),
      height: 40.0,
    );
  }

主要是用到了TextEditingController.fromValue这个功能。

Flutter移动电商,关注公众号:蛇崽网盘资源,回复【Flutter移动电商】,即可领取。

关注「蛇崽网盘教程资源」公众号 ,在微信后台回复「领取资源」,获取IT资源200G干货大全。

更多资源请访问:

超详细图文搭建个人免费博客

关注「蛇崽网盘教程资源」公众号 ,在微信后台回复「领取资源」,获取IT资源200G干货大全。

在微信后台回复「130个小程序」,即可免费领取享有导入就能跑的微信小程序

在这里插入图片描述

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐