簡體   English   中英

Flutter - 如何從其他小部件訪問 DropDown 中當前選定的項目?

[英]Flutter - How can I access current selected item in DropDown from other widget?

我的代碼中有一個DropDown 我需要以某種方式從其他小部件訪問我當前選擇的項目。 如何在其他小部件中訪問此項目? 我可以用 GlobalKey 或構造函數或其他任何東西來做嗎?

我的下拉代碼:

              DropdownButton<String>(
                    items: _myList.map((String dropDownStringItem) {
                      return DropdownMenuItem<String>(
                        value: dropDownStringItem,
                        child: Text(dropDownStringItem),
                      );
                    }).toList(),
                    onChanged: (String newItemSelected) {
                      setState(() {
                        currentItemSelected = newItemSelected;
                      });
                    },
                    value: currentItemSelected,
                  ),

在您的代碼中,您正在 function 中創建一個按鈕,但沒有將其安裝在小部件樹中。 請參閱我的示例代碼,我使用一列來放置下拉菜單和一個按鈕。 每當我對下拉菜單進行更改時,currentItemSelected 都會更改,按鈕文本也會更改。 在按鈕的 onPressed 中,您可以使用導航器通過將 currentItemSelected 值傳遞給其構造函數來導航到另一個頁面。 這樣,您就可以將信息發送到下一頁。

這是一個小例子。 飛鏢例子

暫無
暫無

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

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