如何移除DropdownButtonFormField2默认内边距并匹配容器宽度?
针对你提到的两个需求,这里给出具体的修改方案:
1. 让下拉框宽度与容器一致
通过dropdownStyleData参数设置下拉菜单宽度为double.infinity,使其自动继承父容器的宽度:
dropdownStyleData: DropdownStyleData( width: double.infinity, ),
2. 移除下拉选项默认内边距
在dropdownStyleData中设置itemPadding为EdgeInsets.zero,清除DropdownMenuItem的默认内边距,之后你可以在子组件中自由定义所需内边距:
dropdownStyleData: DropdownStyleData( width: double.infinity, itemPadding: EdgeInsets.zero, ),
修改后的完整代码
Container( width: size.width * .9, child: DropdownButtonFormField2( decoration: InputDecoration( border: UnderlineInputBorder(), labelText: 'Muscle', filled: true, isDense: true, fillColor: Colors.white24, hintStyle: TextStyle(color: Colors.green), focusedBorder: UnderlineInputBorder( borderSide: BorderSide(color: Colors.white), ), ), // 添加下拉菜单样式配置 dropdownStyleData: DropdownStyleData( width: double.infinity, itemPadding: EdgeInsets.zero, ), style: TextStyle(color: Colors.black), value: selectedMuscleGroup, onChanged: (String? newValue) { setState(() { selectedMuscleGroup = newValue; }); }, items: <String>[ 'Biceps', 'Triceps', 'Chest', 'Back', 'Legs', 'Shoulders' ].map<DropdownMenuItem<String>>((String value) { return DropdownMenuItem<String>( value: value, // 这里可自定义你需要的内边距,示例为水平16、垂直8 child: Container( padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8), child: Text(value) ), ); }).toList(), ), ),
内容的提问来源于stack exchange,提问作者Cidiko Andorem
相关产品推荐
相关产品推荐

