【问题标题】:Drop down button value text is not change in flutter/dart下拉按钮值文本在颤动/飞镖中没有变化
【发布时间】:2021-07-20 14:17:23
【问题描述】:

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

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

【问题讨论】:

    标签: android ios flutter dart dropdownbutton


    【解决方案1】:

    你试试:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 很高兴听到它对您有所帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-08
      • 1970-01-01
      • 2021-02-27
      • 2018-05-25
      • 2020-12-16
      • 1970-01-01
      • 2019-02-20
      相关资源
      最近更新 更多