简体   繁体   English

Flutter - 单击显示文本字段时的下拉列表

[英]Flutter - Dropdown list on click display Textfield

I'm quite new in Flutter, so how do I show a textfield widget below right after an user click one of the dropdown list value?我是 Flutter 的新手,所以如何在用户单击下拉列表值之一后立即在下方显示文本字段小部件?

Here is the codes for my Dropdown button这是我的下拉按钮的代码

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),
          ),
        ),
      ),

Here's how it's look now (without the textfield):这是它现在的样子(没有文本字段): 在此处输入图像描述

I've been looking for the solution on the internet but none seems to help me understand.我一直在互联网上寻找解决方案,但似乎没有一个可以帮助我理解。

Thank you in advance!先感谢您!

You can use a visibility widget to show/hide a textfield.您可以使用可见性小部件来显示/隐藏文本字段。 This will be controlled by a state variable, which we will call isVisible.这将由一个状态变量控制,我们称之为 isVisible。 So we have所以我们有

bool isVisible = false;

.....

Visibility(
    visible: isVisible,
    child: TextField(),
);

By default, the textfield won't be shown, but we can make it visible when a dropdown list value is selected.默认情况下,文本字段不会显示,但我们可以在选择下拉列表值时使其可见。

DropdownButtonHideUnderline(
    ....
onChanged: (value) =>
          setState(() {
              this.value = value;
              if (this.value == "Unhealthy") {
                  isVisible = true;
               } else {
                  isVisible = false;
                      }
                })
           );
        
        

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM