首页 > 解决方案 > 清除 TextField 抖动时的错误

问题描述

我正在尝试清除我的 TextField,这就是我所做的

IconButton(
      onPressed: (){
        FocusScope.of(context).requestFocus(new FocusNode());
        _searchFieldController.clear();
        setState(() {
          searchClicked = false;
        });
      },
      icon: Icon(Icons.close),
    );
  }

当我运行它时,我得到了这个错误

I/颤振(4547):══╡手势发现异常╞════════════════════════════════════════════ ════════════════════════════════ I/flutter(4547):处理手势时抛出以下断言:I /flutter(4547):无效文本选择:TextSelection(baseOffset:6,extentOffset:6,affinity:I/flutter(4547):TextAffinity.upstream,isDirectional:false)

这是我的小部件

TextField(
                  onChanged: (text) {
                    if(text.length >= 4){
                      searchResult = productTemp.where((i) => i.productName.contains(text.toString())).toList();
                      _productController.sink.add(searchResult);
                    }else{
                      _productController.sink.add(productTemp);
                    }
                  },
                  focusNode: _focus,
                  controller: _searchFieldController,
                  style: TextStyle(fontSize: 15),
                  decoration: InputDecoration(
                      filled: true,
                      hintStyle:new TextStyle(color: Colors.grey[800],fontSize: 20),
                      fillColor: Colors.white70,
                      hintText: "Search",
                      suffixIcon: searchClicked  ? buildCancelIcon() : buildSearchIcon()
                  ),
                )

标签: flutter

解决方案


https://github.com/flutter/flutter/issues/17647

WidgetsBinding.instance.addPostFrameCallback((_) => _searchFieldController.clear());

推荐阅读