【问题标题】:Flutter dropdown value if not selected, then assign the initial valueFlutter 下拉值如果没有选中,则分配初始值
【发布时间】:2022-01-25 10:40:06
【问题描述】:

我有一个DropDownButtonFormField 我需要检查一下。如果用户没有选择下拉值,则在提交时分配初始值。

自定义下拉菜单

 Container myDropDownContainer(String initialVal, List<String> listItems,
      String text, Function myFunc, Function validate) {
    return Container(
      margin: const EdgeInsets.all(8),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          SizedBox(
            width: 120,
            child: Text(
              text,
              style: kTextStyle,
            ),
          ),
          const SizedBox(
            width: 20,
          ),
          Expanded(
            child: Container(
              height: 50,
              decoration: BoxDecoration(
                  color: Colors.orangeAccent,
                  borderRadius: BorderRadius.circular(5)),
              child: DropdownButtonFormField<String>(
                autovalidateMode: AutovalidateMode.always,
                //menuMaxHeight: 300,
                validator: (value) {
                  if(value!.isEmpty) {
                    return "485s4a8sd4as85";
                  }
                } ,
                decoration: const InputDecoration(border: InputBorder.none),
                isExpanded: true,
                onTap: () => myFunc,
                //borderRadius: BorderRadius.circular(5),
                value: initialVal,
                icon: const Icon(
                  Icons.arrow_downward,
                  color: Colors.black38,
                ),
                iconSize: 24,
                elevation: 16,
                dropdownColor: Colors.deepOrange,
                style: kTextStyle.copyWith(color: Colors.black),
                onChanged: (val) => myFunc(val),
                items: listItems.map<DropdownMenuItem<String>>((String? val) {
                  return DropdownMenuItem(
                    //TODO: Set default values
                    value: val,
                    child: Text(
                      val,
                      style: kTextStyle.copyWith(color: Colors.black),
                    ),
                  );
                }).toList(),
              ),
            ),
          )
        ],
      ),
    );
  }

这是我的onChanged 属性,由用户分配选定的值。我添加了一些关于我想要做什么的解释。

 String _valueCinsiyet = "Diğer"; // initial value
  void onChangedCinsiyet(String? newVal) {
    setState(() {
      if(newVal==null) {
        _formData.setCinsiyet(_valueCinsiyet);
        /*
        'if newVal is null' means that if the value is not selected by the user
        then set the initialValue( _valueCinsiyet)
         */

      } else {
        /*
        if newVal is not null then assign the newVal( which means the selected value)
        into my initialValue, then set the data to use it on different pages. What is missing?
         */
        _valueCinsiyet = newVal;
        _formData.setCinsiyet(_valueCinsiyet);
      }

    });
  }


【问题讨论】:

    标签: flutter dart dropdown


    【解决方案1】:

    您可以使用可为空的数据来跟踪DropdownButtonFormField 的更改。可以为空,您可以检查它是否为空,无需在onChanged: 上进行任何额外操作,只需按常规方式分配新值即可。

    构建方法之前的开启状态: String? value; // value to keep track

    child: DropdownButtonFormField<String>(
      value: value,
      onChanged: (val) {
        setState(() {
          value = val;
        });
      },
    

    现在onSaved/submit 按钮可以通过检查null来传递值,简单的方法是

    value?? "default Value"。在你的情况下,它是value??Diğer

    【讨论】:

    • 如果我如上指定initialValue,initialValue将为null。您能在我的代码中显示确切的更改吗?
    • 你也可以设置初始值,这样你就不会从下拉列表中得到空值,你可以在你的listItemsTODO: value: val ?? "default value" 上包含value: val ?? "default value"
    猜你喜欢
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    • 2018-02-27
    相关资源
    最近更新 更多