Flutter/Android-将焦点从TextField移至DropdownButton [英] Flutter / Android - moving focus from a TextField to a DropdownButton
问题描述
我在屏幕上有一个文本字段和一个下拉按钮.当我从文本字段中选择一个项目然后返回到文本字段时,我觉得这有点尴尬.
I have a textfield and a dropdownbutton on a screen. When I move from the textfield to choose an item and then back to the textfield I find this a little bit awkward.
- 在文本字段中输入
- 通过点击两次来选择下拉列表
我的问题是您必须轻按两次,一次退出文本字段,第二次访问下拉菜单-是否有一种方法可以退出文本字段并一键打开下拉列表?这是内置于Android还是Flutter控件?
My problem is that you have to tap twice, once to exit the textfield and the second to access the dropdown - is there a way to exit the textfield and open the dropdownlist in one tap? Is this built into Android or the Flutter controls?
以下是一些扑朔迷离的代码,它们显示一个下拉列表和一个文本框...
class _TextAndDropdownState extends State<TextAndDropdown> {
int selectedDropdown;
String selectedText;
final textController = new TextEditingController();
@override
void initState() {
super.initState();
selectedDropdown = 1;
textController.addListener(() => print(''));
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Text and dropdown'),
),
body: Container(
child: Column(
children: [
Padding(
padding: EdgeInsets.all(10.0),
),
DropdownButton(value: selectedDropdown, onChanged: _dropdownChange, items: [
DropdownMenuItem(
child: Text('First'),
value: 1,
),
DropdownMenuItem(child: Text('Seconds')),
]),
TextField(controller: textController),
],
),
),
);
}
void _dropdownChange(val) {
setState(() {
selectedDropdown = val;
});
}
}
推荐答案
很抱歉,稍后的答案...我也在为此找到解决方案.现在我明白了.
Sorry for the late Answer... I'm also finding the solution for this. now i get it.
只需添加此内容
FocusScope.of(context).requestFocus(new FocusNode());
在您的
_dropdownChange(val)
方法
void _dropdownChange(val) {
setState(() {
FocusScope.of(context).requestFocus(new FocusNode());///It will clear all focus of the textfield
selectedDropdown = val;
});
}
这篇关于Flutter/Android-将焦点从TextField移至DropdownButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!