I am using DropDownButtonFormField in my code. I want to add margin around it’s items but am not able to do so. Here is my code:
DropdownButtonFormField<String>(
validator: (value) {
if (value == null) {
return AppStrings.pleaseSelectYourGender;
}
return null;
},
borderRadius: BorderRadius.circular(10),
icon: const Icon(Icons.expand_more),
// value: _selectedGender,
items: [
AppStrings.male,
AppStrings.female,
AppStrings.other
].map((String value) {
return DropdownMenuItem<String>(
value: value,
child: SubText1(text: value, color: AppColors.textColor,),
);
}).toList(),
onChanged: (newValue) {
setState(() {
_selectedGender = newValue!;
});
},
decoration: InputDecoration(
labelText: AppStrings.selectGender,
labelStyle: AppStrings.labelStyle,
filled: true,
fillColor: AppColors.fieldColor.withOpacity(0.5),
//border: OutlineInputBorder(),
border: const OutlineInputBorder(
borderSide: BorderSide.none,
),
enabledBorder: const OutlineInputBorder(
borderSide: BorderSide.none,
),
focusedBorder: const OutlineInputBorder(
borderSide: BorderSide.none,
),
// errorBorder: OutlineInputBorder(
// borderSide: BorderSide.none,
// ),
disabledBorder: const OutlineInputBorder(
borderSide: BorderSide.none,
),
),
),
The result I want is:
My layout is:
I want the DropDownMenuItems to be aligned with the textfields above.
2
Answers
Use Padding
1-DropdownButtonFormField has a property called: contentPadding and it is inside of InputDecoration property, you can use it like this :
DropdownButtonFormField(
decoration: InputDecoration(
contentPadding:const EdgeInsets.symmetric(horizontal: 10, vertical: 12),));
2-for the child item (DropdownMenuItem) you can add padding for each widget.