簡體   English   中英

我無法更改下拉按鈕中的提示文本

[英]I can't change the hinttext in the dropdownButton

這是我的代碼,我不知道為什么提示沒有改變,當我單擊選項時,顯然所有這些代碼都在 class 內,我嘗試使用 for 循環來識別列表位置,如果它們是正確的,它會出現在屏幕上。

final listgenre = ["Masc", "Fem", "Não Binário"];
var listgenre_value;
String newValue = "";
var valueChoose;
String hintValue = "";
   Padding(
                padding: EdgeInsets.all(16),
                child: Container(
                    padding: EdgeInsets.only(left: 16, top: 10, right: 15),
                    decoration: BoxDecoration(
                        border: Border.all(color: Colors.grey, width: 2),
                        borderRadius: BorderRadius.circular(20)),
                    child: DropdownButton<String>(
                      hint: Text("Genero :$hintValue"),
                      dropdownColor: Colors.white,
                      icon: Icon(Icons.arrow_drop_down),
                      iconSize: 36,
                      iconEnabledColor: Colors.black,
                      isExpanded: true,
                      style: TextStyle(fontSize: 17, color: Colors.black),
                      value: valueChoose,
                      underline: SizedBox(
                        width: 320,
                        height: 200,
                      ),
                      items: listgenre.map((valueitem) {
                        return DropdownMenuItem(
                          value: valueitem,
                          child: Text(valueitem),
                        );
                      }).toList(),
                      onChanged: (newValue) {
                        setState(() {
                          for (int i = 0; i <= listgenre.length; i++) {
                            if (listgenre[i] != newValue) {
                              listgenre_value = i + 1;
                            } else {
                              hintValue = "$newValue";
                              // ignore: void_checks
                              return listgenre_value;
                            }
                          }

                          Object? valueChoose = newValue;
                          String valueChoosen = valueChoose.toString();
                        });
                      },
                    ))),

根據您的評論,相同的代碼沒有給出預期的輸出。

這是唯一可能的,如果你也被初始化構建方法,它不應該是機箱的變量。

不正確的代碼

@override
  Widget build(BuildContext context) {
    
    // ---- your variables INSIDE build method

    final listgenre = ["Masc", "Fem", "Não Binário"];
    var listgenre_value;
    String newValue = "";
    var valueChoose;
    String hintValue = "";

// ------- rest of the code continued

正確的代碼

  final listgenre = ["Masc", "Fem", "Não Binário"];
  var listgenre_value;
  String newValue = "";
  var valueChoose;
  String hintValue = "";

  @override
  Widget build(BuildContext context) {

  // ------- rest of the code continued

由於每次調用setState都會調用build方法, hintValue被初始化為""即空字符串。

解決此錯誤的正確方法是abdev的評論。 如果您將DropdownButton小部件使用的變量放在build方法之上,將會有所幫助。 我希望它有幫助

暫無
暫無

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

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