如何在下拉式颤振中设置初始默认值

时间:2020-02-24 05:54:52

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

我有一个下拉菜单,下拉按钮内有一些值,默认情况下,我需要选择值。您可以在下面的代码段中找到所需的下拉值。我需要它始终默认情况下具有选定值“正常”。希望你理解这个问题。

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

1 个答案:

答案 0 :(得分:2)

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