Flutter:如何实现下拉列表以在单击中显示Textfield?
我对扑朔迷离的速度相对较新,并且正在尝试实现一个功能,而从下拉列表中选择项目后,就会下方出现Textfield Widget。
这是我的下拉列表的代码段:
final items = ['Healthy', 'Unhealthy'];
...
DropdownMenuItem<String> buildMenuItem(String item) => DropdownMenuItem(
value: item,
child: Text(
item,
style: const TextStyle(
fontSize: 16,
// fontWeight: FontWeight.bold,
fontFamily: "Ubuntu",
color: Colors.black54,
),
),
);
...
Container(
width: scrwidth * 0.8,
height: scrheight / 14,
padding: const EdgeInsets.symmetric(
horizontal: 12, vertical: 4),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.black54, width: 1),
),
child: DropdownButtonHideUnderline(
child: DropdownButton<String>(
icon: const Icon(
Icons.arrow_drop_down,
color: Colors.black54,
),
hint: const Text(
"Health",
style: TextStyle(
fontFamily: "Ubuntu",
fontWeight: FontWeight.bold,
),
),
isExpanded: true,
value: value,
items: items.map(buildMenuItem).toList(),
onChanged: (value) =>
setState(() => this.value = value),
),
),
),
当前,尚未显示Textfield小部件。现在的外观是:
我已经在线搜索了解决方案,但还没有找到解决我特定问题的任何内容。任何帮助将不胜感激!
预先感谢您!
I'm relatively new to Flutter and I'm trying to achieve a functionality where upon selecting an item from a dropdown list, a TextField widget appears below it.
Here's the code snippet for my DropdownButton:
final items = ['Healthy', 'Unhealthy'];
...
DropdownMenuItem<String> buildMenuItem(String item) => DropdownMenuItem(
value: item,
child: Text(
item,
style: const TextStyle(
fontSize: 16,
// fontWeight: FontWeight.bold,
fontFamily: "Ubuntu",
color: Colors.black54,
),
),
);
...
Container(
width: scrwidth * 0.8,
height: scrheight / 14,
padding: const EdgeInsets.symmetric(
horizontal: 12, vertical: 4),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.black54, width: 1),
),
child: DropdownButtonHideUnderline(
child: DropdownButton<String>(
icon: const Icon(
Icons.arrow_drop_down,
color: Colors.black54,
),
hint: const Text(
"Health",
style: TextStyle(
fontFamily: "Ubuntu",
fontWeight: FontWeight.bold,
),
),
isExpanded: true,
value: value,
items: items.map(buildMenuItem).toList(),
onChanged: (value) =>
setState(() => this.value = value),
),
),
),
Currently, the TextField widget is not displayed. Here's how it looks now:
I've searched online for solutions, but haven't found anything that addresses my specific issue. Any help would be greatly appreciated!
Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用可见性小部件显示/隐藏文本字段。这将由状态变量控制,我们将称为可见的。因此,我们
默认情况下,不会显示文本字段,但是当选择下拉列表值时,我们可以使其可见。
You can use a visibility widget to show/hide a textfield. This will be controlled by a state variable, which we will call isVisible. So we have
By default, the textfield won't be shown, but we can make it visible when a dropdown list value is selected.