flutter - 如何在下拉列表中添加分隔符?
问题描述
我有以下下拉列表:
DropdownButtonFormField(
isExpanded: true,
isDense: true,
value: dropdownValue,
icon: SvgPicture.asset(
AppImages.ic_expand_dropdown,
),
iconDisabledColor: AppColors.colorDarkGray,
iconEnabledColor: AppColors.colorDarkGray,
focusNode: widget.focusNode,
validator: widget.validator,
decoration: InputDecoration(
label: widget.widgetLabel,
labelStyle:
widget.labelStyle ?? AppTextStyles.transparentBlueDark16(),
labelText: widget.isLabelRes
? AppLocales.getTextOrNull(widget.label)
: widget.label,
fillColor: Colors.white,
filled: true,
hintText: widget.pickerPlaceholder,
hintStyle: widget.hintStyle ?? AppTextStyles.alto16(),
alignLabelWithHint: true,
errorText: widget.isErrorRes
? AppLocales.getTextOrNull(widget.error)
: widget.error,
contentPadding: const EdgeInsets.symmetric(
horizontal: 0,
vertical: 0,
),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
width: 1,
color:
widget.enabledBorderColor ?? AppColors.colorInputBorder,
),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
width: 2,
color: widget.focusedBorderColor ?? AppColors.colorAccent,
),
),
errorBorder: UnderlineInputBorder(
borderSide: BorderSide(
width: 2,
color: AppColors.colorError,
),
),
border: UnderlineInputBorder(
borderSide: BorderSide(
width: 1,
color: widget.borderColor ?? AppColors.colorInputBorder,
),
),
),
style: AppTextStyles.grayDark16(
fontWeight: AppFontWeight.medium,
),
onChanged: (String? newValue) {
dropdownValue = newValue;
widget.onChanged(dropdownValue!);
},
items: widget.pickerList.map<DropdownMenuItem<String>>((value) {
return DropdownMenuItem<String>(
value: value,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(10),
bottomRight: Radius.circular(10),
),
),
child: Text(
value,
style: AppTextStyles.grayDark16(
fontWeight: AppFontWeight.medium,
),
),
),
Visibility(
child: Divider(
color: AppColors.colorEden10,
height: 2.0,
indent: 10.0,
endIndent: 10.0,
),
visible: widget.pickerList.last != value,
),
],
),
);
}).toList(),
),
我需要显示项目之间的分隔线。由于我没有找到任何将其添加到列表中的选项,因此我通过创建一个包含文本和分隔符的列来添加分隔符,在某些情况下会显示该列。但问题是,当我选择项目分隔符时,结果也会显示在下拉列表中,我看不到任何选项可以在此处隐藏它。有没有办法在选择项目后添加分隔线而不显示它?
解决方案
为避免选中项目分隔符,我们需要使用selectedItemBuilder:
. 默认情况下,它使用items
布局。
你可以使用DropdownButtonFormField
喜欢
selectedItemBuilder: (context) => widget.pickerList
.map(
(text) => Text(text),
)
.toList(),
您可以设置自己的逻辑来创建 UI。简化的状态片段是
late List<DropdownMenuItem<String>> menuItems = [];
@override
void initState() {
super.initState();
dropdownValue = widget.pickerList.first;
for (int i = 0; i < widget.pickerList.length; i++) {
//* on 1st index there will be no divider
i == 0
? menuItems.add(
DropdownMenuItem<String>(
child: Text(
widget.pickerList[i],
),
value: widget.pickerList[i],
),
)
: menuItems.add(
DropdownMenuItem<String>(
child: Container(
width: double.maxFinite,
alignment: Alignment.centerLeft,
padding: const EdgeInsets.only(
top: 6, // adjust the way you like
),
decoration: const BoxDecoration(
border: Border(
top: BorderSide(
color: Colors.grey,
width: 2,
),
),
),
child: Text(
widget.pickerList[i],
),
),
value: widget.pickerList[i],
),
);
}
}
@override
Widget build(BuildContext context) {
return DropdownButtonFormField<String>(
isExpanded: true,
isDense: true,
value: dropdownValue,
onChanged: (String? newValue) {
// dropdownValue = newValue;
// widget.onChanged(dropdownValue!);
},
items: menuItems,
selectedItemBuilder: (context) => widget.pickerList
.map(
(text) => Text(text),
)
.toList(),
);
}
推荐阅读
- amazon-web-services - 更改实例的虚拟化类型
- php - 将 Drupal-7 项目复制到我的服务器后出现空白屏幕
- sql - 避免工会获得 TOP 计数
- c - 将堆存储从 const 转换为非常量会调用未定义的行为吗?
- r - ggplot 中重叠图的图例
- mysql - 使用 Laravel 播种 Mysql 数据库时出现奇怪的错误
- boost-python - Boost numpy 示例不起作用
- powershell - 缺少 Get-AzureStorageShare?
- android - 如何导航回父级?
- ruby-on-rails - 如何在 Ruby on Rails 中使用 ENV 变量验证 Google Vision/Cloud