Flutter DropdownMenu如何缩减边框与文本间的内边距?
你之前用inputDecorationTheme没生效,是因为Material3的DropdownMenu组件内部对输入框样式有默认处理,直接设置的参数可能被覆盖。试试下面两种可行的方法:
方法一:自定义textFieldBuilder直接控制输入框样式
通过textFieldBuilder自定义输入框,直接设置contentPadding来缩小文本与边框的空白:
DropdownMenu<String>( // 自定义输入框构建器,精准控制内边距 textFieldBuilder: (context, controller, focusNode) { return TextField( controller: controller, focusNode: focusNode, decoration: const InputDecoration( isDense: true, // 启用紧凑模式 // 根据需求调整内边距数值,缩小空白 contentPadding: EdgeInsets.symmetric(horizontal: 8, vertical: 4), border: OutlineInputBorder(), // 保留默认边框样式 ), ); }, initialSelection: list.first, onSelected: (String? value) { setState(() { buttonClickedTimes.value = value!; }); }, dropdownMenuEntries: list.map<DropdownMenuEntry<String>>((String value) { return DropdownMenuEntry<String>(value: value, label: value); }).toList(), )
方法二:通过Theme局部覆盖样式
如果不想自定义输入框,可用Theme组件局部覆盖inputDecorationTheme,确保样式生效:
Theme( data: Theme.of(context).copyWith( inputDecorationTheme: const InputDecorationTheme( isDense: true, contentPadding: EdgeInsets.symmetric(horizontal: 8, vertical: 4), ), ), child: DropdownMenu<String>( initialSelection: list.first, onSelected: (String? value) { setState(() { buttonClickedTimes.value = value!; }); }, dropdownMenuEntries: list.map<DropdownMenuEntry<String>>((String value) { return DropdownMenuEntry<String>(value: value, label: value); }).toList(), ), )
调整contentPadding里的数值就能精确控制空白大小,选适合你需求的数值即可。
内容的提问来源于stack exchange,提问作者Abhishek Kumar
相关产品推荐
相关产品推荐

