简体   繁体   English

Flutter:下拉按钮未更新以显示所选选项

[英]Flutter: Dropdownbutton does not update to display selected choice

I am trying to use a dropdown button to have the user select from a list of options, however after making a selection the dropdown button remains displaying the hint.我正在尝试使用下拉按钮让用户 select 从选项列表中,但是在做出选择后,下拉按钮仍然显示提示。 I think something about the setState is not updating the dropdown button.我认为 setState 的某些内容没有更新下拉按钮。

              value: skillChoice,
              items: listDrop,
              hint: Text("Choose Skill"),
              onChanged: (value) {

                setState(() {
                  skillChoice = value;
                });
              },
            ),

here are the variables which are declared earlier in the code:以下是代码中前面声明的变量:

      List<DropdownMenuItem<int>> listDrop = [];
      int skillChoice = null;

Can anyone let me know why it isn't updating?谁能告诉我为什么不更新?

I think setting skillChoice null initially disables the dropdown.我认为设置 SkillChoice null 最初会禁用下拉菜单。

It would have been better if you had shown the full code snippet of how you implemented your DropDownButton.如果您展示了如何实现 DropDownButton 的完整代码片段,那就更好了。 But here is how I do implement mine:但这是我如何实现我的:

 // This is the initial value that will be selected on the DropDownMenu by default

    String choice = '1';


    // This is the List of String (or whatever data type you want) that will make up the 
    Drop Down Menu Item options. NOTE: That the String value of 'choice' ('1') is also present in the List of choices ('1')

    List<String> choices = [
    '1',
    '2',
    '3',
    '4',
    '5',
    ];

    DropdownButton<String>(
                value: choice,
                icon: Icon(Icons.add),
                onChanged: (String newValue) {
                  setState(() => choices = newValue);
                },
                items: choices.map<DropdownMenuItem<String>>((String value) {
                  return DropdownMenuItem<String>(
                    value: value,
                    child: Text(
                          value,
                        )
                  );
                }).toList(),
              ),

Should work properly for you now.现在应该为您正常工作。

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

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