Drop down button in flutter not switching values to the selected value

前端 未结 2 2012
梦毁少年i
梦毁少年i 2021-02-07 11:20

I\'ve recently started programming using dart and flutter and everything has been going smoothly for my app, although recently i wanted to add drop down menu to provide the user

2条回答
  •  死守一世寂寞
    2021-02-07 11:59

    The error is because you are declaring a method variable newValue you must declare that variable as global inside your StatefulWidget.

       String newValue;
    
      Widget buildDropdownButton() {
      return new Padding(
        padding: const EdgeInsets.all(24.0),
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            new ListTile(
              title: const Text('Frosting'),
              trailing: new DropdownButton(
                  hint: Text('Choose'),
                  onChanged: (String changedValue) {
                    newValue=changedValue;
                    setState(() {
                      newValue;
                      print(newValue);
                    });
                  },
                  value: newValue,
                  items: ['None', 'Chocolate', 'Vanilla', 'ButterCream']
                      .map((String value) {
                    return new DropdownMenuItem(
                      value: value,
                      child: new Text(value),
                    );
                  }).toList()),
            ),
          ],
        ),
      );
      }
    

提交回复
热议问题