I spent lot of time trying to fix the size of the expanded area (when you open the DropdownButtonFormField), but it always take the full width of the screen
My code is:
DropdownButtonFormField(
decoration: InputDecoration(
prefixIcon: Icon(Icons.person),
enabledBorder: OutlineInputBorder(
borderRadius: const BorderRadius.all(Radius.circular(RADIUS),),
borderSide: BorderSide(color: Theme.of(context).disabledColor),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(RADIUS),),
borderSide: BorderSide(color: Theme.of(context).primaryColor),
),
isDense: true,
),
hint: Text('gender'),
isExpanded: true,
isDense: true,
items: ['Male', 'Female'].map((String value) {
return DropdownMenuItem(
value: value,
child: Text(value),
);
}).toList(),
onChanged: (_) {
},
),
2
Answers
If you want to specify a specific width for the DropdownMenuItem within a DropdownButtonFormField in Flutter, you can use a combination of Container or SizedBox with a fixed width value. This approach allows you to control the width of each dropdown item individually. Here’s how you can do it:
By default, dropdown menus have extra padding on both sides, making them wider than the button. You can achieve a consistent width with below approach:
Set
ButtonThemeData.alignedDropdown
totrue
in your button theme. This property specifically adjusts the dropdown menu width to match the button’s width.Here’s an example:
Here’s a full example as per your requirement:
Explanation:
alignedDropdown
is set totrue
, the button’s existingstart
andend
padding (typically 16px and 24px) is automatically applied to the dropdown menu. This ensures the content within the button and menu items aligns horizontally.