如何在AutoCompleteTextField中设置值?
我有如何在AutoCompleteTextField中设置值?,autocomplete,flutter,autosuggest,Autocomplete,Flutter,Autosuggest,我有textfield,它使用autocomplete\u textfieldpackage,在用户键入时显示自动完成建议。现在我想在textfield中显示所选的建议,在itemsmitted()方法中,我使用setState(){currentText=item.}在控制台中正确打印值,但我不确定如何在textfield中显示该值。如果我没有错,我需要使用TextEditingController在textfield中检索和设置值,但不确定如何在AutoCompleteTextField中使
textfield
,它使用autocomplete\u textfield
package,在用户键入时显示自动完成建议。现在我想在textfield
中显示所选的建议,在itemsmitted()
方法中,我使用setState(){currentText=item.}
在控制台中正确打印值,但我不确定如何在textfield
中显示该值。如果我没有错,我需要使用TextEditingController
在textfield
中检索和设置值,但不确定如何在AutoCompleteTextField
中使用TextEditingController
当前代码如下:
@override
void initState() {
_loadData();
textField = AutoCompleteTextField<Categories>(
style: new TextStyle(color: Colors.white, fontSize: 16.0),
decoration: new InputDecoration(
suffixIcon: Container(
width: 85.0,
height: 60.0,
color: Colors.green,
child: new IconButton(
icon: new Image.asset(
'assets/search_icon_ivory.png',
color: Colors.white,
height: 18.0,
),
onPressed: () {},
),
),
fillColor: Colors.black,
contentPadding: EdgeInsets.fromLTRB(10.0, 30.0, 10.0, 20.0),
filled: true,
hintText: 'Search',
hintStyle: TextStyle(color: Colors.white)),
itemSubmitted: (item) {
setState(() {
currentText = item.autocompleteterm;
print(currentText);
});
},
submitOnSuggestionTap: true,
clearOnSubmit: true,
textChanged: (item) {
},
key: key,
suggestions: CategoryViewModel.categories,
textInputAction: TextInputAction.go,
itemBuilder: (context, item) {
return new Container(
color: Colors.black87,
child: Padding(
padding: EdgeInsets.all(8.0),
child: new Text(item.autocompleteterm,
style: TextStyle(
color: Colors.white70,
fontSize: 16.0
)),
),
);
},
itemSorter: (a, b) {
return a.autocompleteterm.compareTo(b.autocompleteterm);
},
itemFilter: (item, query) {
return item.autocompleteterm
.toLowerCase()
.startsWith(query.toLowerCase());
},
);
super.initState();
_getUser();
}
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomPadding: false,
backgroundColor: Color(0xFF13212C),
appBar: AppBar(
title: Text('Demo'),
),
drawer: appDrawer(),
body: new Center(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
new Column(children: <Widget>[
textField,
]),
@覆盖
void initState(){
_loadData();
textField=AutoCompleteTextField(
样式:新文本样式(颜色:Colors.white,fontSize:16.0),
装饰:新的输入装饰(
容器(
宽度:85.0,
身高:60.0,
颜色:颜色。绿色,
孩子:新的图标按钮(
图标:new Image.asset(
'资产/搜索图标\象牙.png',
颜色:颜色,白色,
身高:18.0,
),
按下:(){},
),
),
fillColor:Colors.black,
contentPadding:EdgeInsets.fromLTRB(10.0,30.0,10.0,20.0),
是的,
hintText:'搜索',
hintStyle:TextStyle(颜色:Colors.white)),
已提交项目:(项目){
设置状态(){
currentText=item.autocompleteterm;
打印(当前文本);
});
},
submitOnSuggestionTap:正确,
clearOnSubmit:是的,
文本更改:(项目){
},
钥匙:钥匙,
建议:CategoryViewModel.categories,
textInputAction:textInputAction.go,
itemBuilder:(上下文,项){
退回新货柜(
颜色:颜色。黑色87,
孩子:填充(
填充:边缘设置。全部(8.0),
子项:新文本(item.autocompleteterm,
样式:TextStyle(
颜色:颜色。白色70,
字体大小:16.0
)),
),
);
},
物品分拣员:(a、b){
返回a.autocompleteterm.compareTo(b.autocompleteterm);
},
itemFilter:(项目,查询){
return item.autocompleteterm
.toLowerCase()
.startsWith(query.toLowerCase());
},
);
super.initState();
_getUser();
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
resizeToAvoidBottomPadding:false,
背景颜色:颜色(0xFF13212C),
appBar:appBar(
标题:文本(“演示”),
),
抽屉:appDrawer(),
正文:新中心(
子:新列(
crossAxisAlignment:crossAxisAlignment.stretch,
儿童:[
新栏目(儿童:[
textField,
]),
这可以通过以下方式实现:
itemSubmitted: (item) {
setState(() => textField.textField.controller.text = item.autocompleteterm);
},
并使
clearOnSubmit:false
能够在文本字段中显示所选值。这可以通过如下方式实现:
itemSubmitted: (item) {
setState(() => textField.textField.controller.text = item.autocompleteterm);
},
并使
clearOnSubmit:false
能够在textfield中显示所选值。Yep,clearOnSubmit:false是meYep缺少的值,clearOnSubmit:false是我缺少的值