我正在尝试清除我的TextField,这是我的工作
IconButton(
onPressed: (){
FocusScope.of(context).requestFocus(new FocusNode());
_searchFieldController.clear();
setState(() {
searchClicked = false;
});
},
icon: Icon(Icons.close),
);
}
我运行它时收到此错误
I / flutter(4547):GE手势引起的异常 ╞═════════════════════════════════════════════════ ══════════════════ I / flutter(4547):处理以下内容时引发了以下断言 手势:I / flutter(4547):无效的文本选择: TextSelection(baseOffset:6,scopeOffset:6,亲和力: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()
),
)
答案 0 :(得分:0)
https://github.com/flutter/flutter/issues/17647
WidgetsBinding.instance.addPostFrameCallback((_) => _searchFieldController.clear());
答案 1 :(得分:0)
参考:https://github.com/flutter/flutter/issues/35848
Future.delayed(
Duration(milliseconds: 50),
).then(
(_) {
_suburbSearchController.clear();
},
);
在上面的参考中,有两种方法,包括WidgetsBinding
。
我更喜欢delayed
,因为有人在WidgetsBinding
解决方法中提到“ ...多次添加相同的回调(例如,每次构建窗口小部件时都添加一个侦听器)...”。>