Flutter:如何实现下拉列表以在单击中显示Textfield?

发布于 2025-02-13 00:21:42 字数 1826 浏览 1 评论 0原文

我对扑朔迷离的速度相对较新,并且正在尝试实现一个功能,而从下拉列表中选择项目后,就会下方出现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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼泪也成诗 2025-02-20 00:21:42

您可以使用可见性小部件显示/隐藏文本字段。这将由状态变量控制,我们将称为可见的。因此,我们

bool isVisible = false;

.....

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

默认情况下,不会显示文本字段,但是当选择下拉列表值时,我们可以使其可见。

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

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

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;
                      }
                })
           );
        
        
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文