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
});
},
),
),
);
Comments
-
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 about 4 years
value: field.value
this is the currentDropdownButton
value -
Rutvik Gumasana about 4 yearsyes but it won't select by the default value. I need by default intial value
-
pskink about 4 yearsbefore the
build
method is called just set the default value tofield.value
and yourDropdownButton
will show your default -
Rutvik Gumasana about 4 yearscan you please show me how can I do it??
-
pskink about 4 years
field.value = 'urgent'
-
Rutvik Gumasana about 4 yearsyes, 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 about 4 yearsthere 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 about 4 yearshey thank you so much men it just worked. :)
-
pskink about 4 yearsnext time, read the official documentation first, and then write your code ;-)
-