How to set initial default value in drop down flutter

6,713

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
                  });
                },
              ),
            ),
          );
Share:
6,713
Rutvik Gumasana
Author by

Rutvik Gumasana

flutterrun.com

Updated on November 26, 2022

Comments

  • Rutvik Gumasana
    Rutvik Gumasana over 1 year

    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;
                        },
                      ),
                    ),
                  );
                },
              )),
        );
    
    • pskink
      pskink about 4 years
      value: field.value this is the current DropdownButton value
    • Rutvik Gumasana
      Rutvik Gumasana about 4 years
      yes but it won't select by the default value. I need by default intial value
    • pskink
      pskink about 4 years
      before the build method is called just set the default value to field.value and your DropdownButton will show your default
    • Rutvik Gumasana
      Rutvik Gumasana about 4 years
      can you please show me how can I do it??
    • pskink
      pskink about 4 years
      field.value = 'urgent'
    • Rutvik Gumasana
      Rutvik Gumasana about 4 years
      yes, but how can I put into the code because this is the default variable of the dropdown. can you please show this into the answer section so that if it works i can mark as a complete :)
    • pskink
      pskink about 4 years
      there is some sample code here: api.flutter.dev/flutter/material/DropdownButton-class.html - it has String dropdownValue = 'One'; line where the default is set
    • Rutvik Gumasana
      Rutvik Gumasana about 4 years
      hey thank you so much men it just worked. :)
    • pskink
      pskink about 4 years
      next time, read the official documentation first, and then write your code ;-)