How to set initial default value in drop down flutter

Viewed 8457

I've one drop down and there some value inside the drop-down button and I need to by default selected value. you can seel below piece of snippet where you can find the drop-down value. I need it always there is by default selected value Normal. hope you understand the question.

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 Answers

Just Asign on initState()

selectedDropDownValue = "normal";

In DropDown, asign selectedDropDownValue to value, and update on onChanged callback

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