0

我的代码有问题,我定义了一个下拉按钮菜单,它会出现参数,当我单击参数时,它将选择该值,它不会在下拉按钮中改变。加上我的代码是在有状态小部件中定义的。这是我的代码:

String dropdownValue;
return DropdownButton<String>(
                value: dropdownValue,
                //icon: const Icon(Icons.arrow_downward,),
                iconSize: 22,
                elevation: 16,
                style: TextStyle(color: Colors.black),
                underline: Container(
                  height: 2,
                  color: Colors.black,
                ),
                isDense: true,
                hint: Padding(
                  padding: EdgeInsets.only(right: MediaQuery.of(context).size.width / 36),
                  child: Text('انتخاب موقعیت',
                  style: TextStyle(
                    fontFamily: "IranSans"
                  ),
                  ),
                ),
                onChanged: (String newValue) {
                  setState(() {
                    dropdownValue = newValue;
                  });
                },
                items: <String>['One', 'Two', 'Three', 'Four', 'Five']
                    .map<DropdownMenuItem<String>>((String value) {
                  return DropdownMenuItem<String>(
                    value: value,
                    child: Text(value),
                  );
                }).toList(),
              ),
4

2 回答 2

0

您在本地状态中声明变量。取而代之的是,您应该全局声明变量。

class DropDown extends StatefulWidget {
@override
_DropDowneState createState() => 
_DropDownState();
}
class _DropDownState extends State<DropDown> {

String dropdownValue;

  @override
Widget build(BuildContext context) {
return DropdownButton<dynamic>(
  value: dropdownValue,

  //icon: const Icon(Icons.arrow_downward,),
  iconSize: 22,
  elevation: 16,
  style: TextStyle(color: Colors.black),
  underline: Container(
    height: 2,
    color: Colors.black,
  ),
  isDense: true,
  hint: Padding(
    padding: EdgeInsets.only(right: MediaQuery.of(context).size.width / 36),
    child: Text(
      'انتخاب موقعیت',
      style: TextStyle(fontFamily: "IranSans"),
    ),
  ),

  items: <String>['One', 'Two', 'Three', 'Four', 'Five']
      .map<DropdownMenuItem<dynamic>>((String value) {
    return DropdownMenuItem<dynamic>(
      value: value,
      child: Text(value),
    );
  }).toList(),
  onChanged: (newValue) {
    setState(() {
      dropdownValue = newValue;
      print(dropdownValue);
    });
  },
 );
}}
于 2021-04-27T10:22:43.437 回答
0

你试试:

String dropdownValue = 'One';
return DropdownButton<String>(
  value: dropdownValue,
  icon: const Icon(Icons.arrow_downward),
  iconSize: 24,
  elevation: 16,
  style: const TextStyle(color: Colors.deepPurple),
  underline: Container(
    height: 2,
    color: Colors.deepPurpleAccent,
  ),
  onChanged: (String? newValue) {
    setState(() {
      dropdownValue = newValue!;
    });
  },
  items: <String>['One', 'Two', 'Free', 'Four']
      .map<DropdownMenuItem<String>>((String value) {
    return DropdownMenuItem<String>(
      value: value,
      child: Text(value),
    );
  }).toList(),
);
于 2021-04-27T09:46:54.627 回答