0

我正在尝试使用下拉按钮让用户从选项列表中进行选择,但是在做出选择后,下拉按钮仍然显示提示。我认为 setState 的某些内容没有更新下拉按钮。

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

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

以下是代码中前面声明的变量:

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

谁能告诉我为什么不更新?

4

2 回答 2

0

如果您展示了如何实现 DropDownButton 的完整代码片段,那就更好了。但这是我如何实现我的:

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

现在应该为您正常工作。

于 2020-06-15T22:35:03.983 回答
0

我认为将 SkillChoice 设置为 null 最初会禁用下拉菜单。

于 2020-06-15T19:46:26.487 回答