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;
},
),
),
);
},
)),
);
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
How to customize the appearance of a DropDownButton?
I would like to do a few things to make the drop down looks like a Text Entry:
remove the bottom line inside the yellow box
indent "Use Email" so it left aligns with "Email Address"
make the drop down the same size as the text box (default size)
put the down arrow close to the right hand side of the drop down
add borders
Container(
width: double.infinity,
decoration: BoxDecoration(
color: Colors.yellow[600],
borderRadius: BorderRadius.circular(4),
),
child: DropdownButton(
value: DropDownValue,
icon: const Icon(Icons.keyboard_arrow_down),
items: DropDownItems.map((String DropDownItems) {
return DropdownMenuItem(
value: DropDownItems,
child: Text(DropDownItems),
);
}).toList(),
onChanged: (String? newValue) {
setState(() {
DropDownValue = newValue!;
if (DropDownValue == 'Use Email') {
RegisterType = 'Email';
} else {
RegisterType = 'Mobile';
}
});
},
),
),
SizedBox(height: 10),
TextField(
decoration: InputDecoration(
border: OutlineInputBorder(), labelText: RegisterType),
controller: _EmailAddressController,
),
Try this code:
Container(
width: double.infinity,
decoration: BoxDecoration(
color: Colors.yellow[600],
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(4),
),
child: DropdownButtonHideUnderline(
child: ButtonTheme(
alignedDropdown: true,
child: DropdownButton(
value: DropDownValue,
icon: const Icon(Icons.keyboard_arrow_down),
items: DropDownItems.map((String DropDownItems) {
return DropdownMenuItem(
value: DropDownItems,
child: Text(DropDownItems),
);
}).toList(),
onChanged: (String? newValue) {
setState(() {
DropDownValue = newValue!;
if (DropDownValue == 'Use Email') {
RegisterType = 'Email';
} else {
RegisterType = 'Mobile';
}
});
},
),
),
),
),
SizedBox(height: 10),
TextField(
decoration: InputDecoration(
border: OutlineInputBorder(), labelText: RegisterType),
controller: _EmailAddressController,
),
I would like that in my dropdown at the beginning no value is entered. only if I select the value it should be displayed in the dropdown. if I comment out "value: _selected" is not filled but after the onChanged also nothing is filled.
FormField<Device>(
initialValue: _selected,
builder: (field) {
return InputDecorator(
decoration: InputDecoration(
labelText: widget.labelText,
border: const OutlineInputBorder(),
),
child: DropdownButtonHideUnderline(
child: DropdownButton<Device>(
value: _selected,
isDense: true,
isExpanded: true,
items: widget.list
.map<DropdownMenuItem<Device>>((Device value) {
return DropdownMenuItem<Device>(
value: value,
child:
Text('${value.inventarnr} - ID: ${value.id}'),
);
}).toList(),
onChanged: (value) {
setState(() {
_selected = value;
});
},
),
),
);
},
),
Below worked example for your reference.
initializing default dropdown value
String phaseInitialValue = "Select dropdown";
declare a class to get the user selected value from dropdown.
I have using temporary json data...
List<Phase> phaseList = phaseJsonList.phaseList;
Phase phase = Phase("","");
FormField full code :
FormField(
initialValue: phaseInitialValue,
builder: (field) {
return InputDecorator(
decoration: const InputDecoration(
labelText: "Dropdown",
border: OutlineInputBorder(),
),
child: DropdownButtonHideUnderline(
child: DropdownButton(
hint: Text(
phaseInitialValue,
style: const TextStyle(
fontWeight: FontWeight.w400,
color: Color.fromRGBO(179, 179, 179, 1.0),
fontSize: 15.0,
fontStyle: FontStyle.normal
),
),
isExpanded: true,
iconSize: 30.0,
iconEnabledColor:
const Color.fromRGBO(0, 0, 0, 1.0),
style: const TextStyle(
color: Color.fromRGBO(0, 0, 0, 1)),
dropdownColor:
const Color.fromRGBO(255, 255, 255, 1.0),
borderRadius: BorderRadius.circular(5),
items: phaseList.map(
(phaseData) {
return DropdownMenuItem<Phase>(
value: phaseData,
child: Text(phaseData.phaseName),
);
},
).toList(),
onChanged: (Phase? newData) {
setState(() {
phase = newData!;
phaseInitialValue = newData.phaseName!;
},
);
},
),
),
);
},
),
if user changed the value from dropdown i have retrieve that from onChanged property. there i changed inital text to user selected object...
you can able to fetch actual id as well as object...
have a good day!!!
How can I add the underline for DropdownButton items, I have tried underline like below but it didn't work
DropdownButton<String>(
isExpanded: true,
hint: Text("Status"),
value: value.selected,
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
items: items
.map((key, value) {
return MapEntry(
key,
DropdownMenuItem<String>(
value: key,
child: Text(value),
));
})
.values
.toList(),
onChanged: (String val) {
value.selected = val;
},
)
SizedBox(
width: 100.0,
child: DropdownButtonFormField<int>(
decoration: InputDecoration(
border: UnderlineInputBorder(
borderSide:
BorderSide(color: Colors.white))),
value: 2,
items: <DropdownMenuItem<int>>[
DropdownMenuItem<int>(
value: 1,
child: Text("Owner"),
),
DropdownMenuItem<int>(
value: 2,
child: Text("Member"),
),
],
),
I have a dropdown list in my flutter app that has about 30 items in it. When it is selected I see the values but it takes up whole screen.
How can I control the height of the selected list so that it does not overlay the entire screen (maybe show 10 items with a scroll)
Here is my code
Widget buildCategoryFormField(candidateModel) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("Job category"),
SizedBox(
height: 5,
),
FormField(
builder: (FormFieldState state) {
return InputDecorator(
decoration: InputDecoration(
border: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.grey,
),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.grey,
),
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.blue,
),
),
// hintText: "firstname *",
// labelText: "job category",
errorText: state.hasError ? state.errorText : null,
labelStyle: TextStyle(
color: Colors.grey,
),
),
isEmpty: _selectedjobCategory == '',
child: new DropdownButtonHideUnderline(
child: new DropdownButton(
style: TextStyle(fontSize: 18, color: Palette.GREY),
value: _selectedjobCategory,
isDense: true,
onChanged: (String newValue) {
setState(() {
_selectedjobCategory = newValue;
state.didChange(newValue);
});
},
items: JOB_CATEGORY.map((String value) {
return new DropdownMenuItem(
value: value,
child: new Text(value),
);
}).toList(),
),
),
);
},
validator: (val) => validateJobCategory(val),
),
],
);
}
You can define menuMaxHeight on DropdownButton to set maximum height of the expanded dropdown list items.
Expanded(
/// LayoutBuilder can be used to fetch the parent widget's dimensions
child: LayoutBuilder(builder: (context, constraints) {
return Center(
child: DropdownButton<String>(
/// Set dropdown list max height
menuMaxHeight: constraints.maxHeight,
value: dropdownValue,
items: dropdownItems,
),
);
}
)
Adding pagination to the dropdown list is feasible, but this will require creating a custom DropdownButton class. Design-wise, it would be better to break the list into smaller chunks (i.e. by categories) if you'll need to display >50 items in the dropdown.
Can a Dropdown Button:
return DropdownButton<String>(
items: <String>['Foo', 'Bar'].map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text(value),
);
}).toList(),
onChanged: (_) {},
);
have something similar to a decoration user in a TextFormField:
TextFormField(
controller: _titleController,
decoration: InputDecoration(labelText: 'Input'),
validator: (String value) {
if (value != null && value.isEmpty) {
return 'Please enter some text';
}
},
style: Theme.of(context).textTheme.title,
),
When something is written in the above TextFormField the word Input shows. Like this:
Replace DropdownButton with DropdownButtonFormField:
https://api.flutter.dev/flutter/material/DropdownButtonFormField-class.html
Change DropdownButton to DropdownButtonFormField and add this decoration....
decoration: InputDecoration(
filled: true,
fillColor: Hexcolor('#ecedec'),
labelText: 'Occupation',
border: new CustomBorderTextFieldSkin().getSkin(),
),
Copy Paste and see magic
I have done flutter dropdown with material design
Padding(
padding: const EdgeInsets.all(9.0),
child: InputDecorator(
decoration: InputDecoration(
labelText: 'Priority',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0)),
contentPadding: EdgeInsets.all(10),
),
child: ButtonTheme(
materialTapTargetSize: MaterialTapTargetSize.padded,
child: DropdownButton<String>(
hint: const Text("Priority"),
isExpanded: true,
value: dropdownValue,
elevation: 16,
underline: DropdownButtonHideUnderline(
child: Container(),
),
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
});
},
items: <String>['One', 'Two', 'Free', 'Four']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
),
),
),