问题描述
当用户单击texform字段时,我试图显示该对话框,但该对话框显示在屏幕中央而不是textformfield上?请帮助我。
解决方法
您可以使用DropDownButton
而不是对话框来实现目标
enter link description here
您可以使用TypeAheadFormField包将文本字段用作文本字段以及下拉菜单。请找到示例代码示例
TypeAheadFormField(
textFieldConfiguration: TextFieldConfiguration(
controller: textFieldController,autofocus: false,textAlign: TextAlign.center,onSelection: (selectedItem) {
print(selectedItem);
},decoration: InputDecoration(
isDense: true,fillColor: Color(0xFFEAEAEA),filled: true,border: OutlineInputBorder(
borderSide: BorderSide.none,),keepSuggestionsOnLoading: false,suggestionsCallback: (pattern) => getSuggestions(searchList,pattern),itemBuilder: (context,suggestion) => ListTile(
title: Text(suggestion),transitionBuilder: (context,suggestionsBox,controller) =>
suggestionsBox,onSuggestionSelected: (suggestion) {
if (onSelection != null) {
onSelection(suggestion);
}
textFieldController.text = suggestion;
},getImmediateSuggestions: true,)
List<String> getSuggestions(List<String> searchList,String query) {
final filtered = searchList
.where((item) => item.toLowerCase().contains(query.toLowerCase()));
return filtered.toList();}
其中searchList是要在文本字段上显示的列表。
,尝试一下,我认为这对您有帮助!
class _YourPageState extends State<YourPage> {
Map yourJson = {
"status": true,"message": "success","data": {
"list": [
{"idattribute": "2","attrName": "BBQ"},{"idattribute": "1","attrName": "FRUIT JUICE"}
]
}
};
int _value = 1;
List<DropdownMenuItem<int>> _menuItems;
@override
void initState() {
super.initState();
List dataList = yourJson["data"]["list"];
_menuItems = List.generate(
dataList.length,(i) => DropdownMenuItem(
value: int.parse(dataList[i]["idattribute"]),child: Text("${dataList[i]["attrName"]}"),);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: DropdownButton<int>(
items: _menuItems,value: _value,onChanged: (value) => setState(() => _value = value),);
}
}