flutter - 清除 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()
),
)
解决方案
https://github.com/flutter/flutter/issues/17647
WidgetsBinding.instance.addPostFrameCallback((_) => _searchFieldController.clear());
推荐阅读
- jquery - jquery:取消绑定一个特定的函数,留下另一个
- google-cloud-platform - 什么 gcloud 命令可以识别项目中对所有 API 具有完全访问权限的 VM 实例?
- python - django 中的自定义用户模型。唯一约束失败:users_user.username
- c# - 将图像转换为 base 64 字符串 xamarin
- javascript - 为什么我的 setState 不能使用带有 react-native 的 useEffect 在 setInterval 中更新?
- javascript - 当用户点击输入时,如何在具有多个按钮的表单中触发正确的表单操作?
- arrays - 谷歌电子表格数组公式比较加标题和空白
- c++ - 在同一个套接字上同时调用 Boost C++ ASIO 函数 close() 和 async_write() 是否有任何问题?
- c++ - 如何修改二叉树?
- c# - 为什么 apicontroller 中的路由不起作用?