简体   繁体   中英

I can't change the hinttext in the dropdownButton

This is my code, and i dont know why the hint doesnt change, when i click the options, obviously all this code is inside a class, I tried to use a for loop to identify the list positions, and if they were right, it would appear on the screen.

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();
                        });
                      },
                    ))),

As per your comments, the same code doesn't give the expected output.

It is only possible if you are also initialising the variables inside the build method, which shouldn't be the case.

Incorrect Code

@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

Correct Code

  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

As the build method is called every time setState is called, hintValue was being initialised to "" ie empty string.

The correct way to fix this error is abdev 's comment. It will help if you put the variables used by the DropdownButton widget above the build method. I hope it was helpful

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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