【问题标题】:How to set initial default value in drop down flutter如何在下拉颤动中设置初始默认值
【发布时间】:2020-06-07 18:00:41
【问题描述】:

我有一个下拉菜单,下拉按钮中有一些值,默认情况下我需要选择值。您可以在 sn-p 下方看到下拉值。我需要它总是默认选择值正常。希望你理解这个问题。

FormBuilder(
      autovalidate: autovalidate,
      child: FormBuilderCustomField(
          attribute: "Select Address",
          validators: [
            FormBuilderValidators.required(),
          ],
          formField: FormField(
            builder: (FormFieldState<dynamic> field) {
              return InputDecorator(
                decoration: InputDecoration(
                  errorText: field.errorText,
                  filled: false,
                  isDense: true,
                  border: InputBorder.none,
                  icon: Container(
                    width: 50.0,
                    height: 50.0,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(20.0),
                      color: colorStyles['primary_light'],
                    ),
                    child: Icon(
                      Icons.business_center,
                      color: colorStyles['primary'],
                    ),
                  ),
                ),
                isEmpty: _typeValue == [],
                child: new DropdownButtonHideUnderline(
                  child: DropdownButton(
                    hint: Text("Service Type"),
                    isExpanded: true,
                    items: [
                      "normal",
                      "urgent",
                      "emergency",
                    ].map((option) {
                      return DropdownMenuItem(
                        child: Text("$option"),
                        value: option,
                      );
                    }).toList(),
                    value: field.value,
                    onChanged: (value) {
                      field.didChange(value);
                      _serviceType = value;
                    },
                  ),
                ),
              );
            },
          )),
    );

【问题讨论】:

  • value: field.value 这是当前的DropdownButton
  • 是的,但默认值不会选择。我需要默认初始值
  • 在调用build 方法之前,只需将默认值设置为field.value,您的DropdownButton 将显示您的默认值
  • 你能告诉我我该怎么做吗??
  • 这里有一些示例代码:api.flutter.dev/flutter/material/DropdownButton-class.html - 它有 String dropdownValue = 'One'; 设置默认值的行

标签: flutter dart drop-down-menu flutter-layout flutter-test


【解决方案1】:

只需在 initState() 上赋值

selectedDropDownValue = "normal";

在 DropDown 中,将 selectedDropDownValue 分配给 value,并在 onChanged 回调上更新

new DropdownButtonHideUnderline(
              child: DropdownButton(
                hint: Text("Service Type"),
                isExpanded: true,
                items: [
                  "normal",
                  "urgent",
                  "emergency",
                ].map((option) {
                  return DropdownMenuItem(
                    child: Text("$option"),
                    value: option,
                  );
                }).toList(),
                value: selectedDropDownValue, //asign the selected value
                onChanged: (value) {
                setState((){
                 selectedDropDownValue = value; //on selection, selectedDropDownValue i sUpdated
                  });
                },
              ),
            ),
          );

【讨论】:

    猜你喜欢
    • 2020-01-12
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-20
    • 2021-12-16
    • 2021-10-18
    • 2018-09-29
    相关资源
    最近更新 更多