簡體   English   中英

Flutter:下拉按鈕未更新以顯示所選選項

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

我正在嘗試使用下拉按鈕讓用戶 select 從選項列表中,但是在做出選擇后,下拉按鈕仍然顯示提示。 我認為 setState 的某些內容沒有更新下拉按鈕。

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

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

以下是代碼中前面聲明的變量:

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

誰能告訴我為什么不更新?

我認為設置 SkillChoice null 最初會禁用下拉菜單。

如果您展示了如何實現 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(),
              ),

現在應該為您正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM