Flutter 自定义下拉按钮和菜单项颤振
我正在尝试用分离和浓缩的菜单项构建自己的下拉按钮,如下图所示: 这是我到目前为止尝试过的代码,我得到了与容器匹配的下拉宽度,但到目前为止无法自定义项目,并且高度始终从按钮上方开始,不占用容器的宽度:Flutter 自定义下拉按钮和菜单项颤振,flutter,drop-down-menu,menuitem,Flutter,Drop Down Menu,Menuitem,我正在尝试用分离和浓缩的菜单项构建自己的下拉按钮,如下图所示: 这是我到目前为止尝试过的代码,我得到了与容器匹配的下拉宽度,但到目前为止无法自定义项目,并且高度始终从按钮上方开始,不占用容器的宽度: body: Container( margin: EdgeInsets.symmetric(horizontal: 10.0, vertical: 5.0), child: Container( width: double.infinity, decorat
body: Container(
margin: EdgeInsets.symmetric(horizontal: 10.0, vertical: 5.0),
child: Container(
width: double.infinity,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
border: Border.all(color: Colors.brown, width: 1.0)),
padding: EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
child: DropdownButtonHideUnderline(
child: ButtonTheme(
alignedDropdown: true,
child: DropdownButton(
isExpanded: true,
isDense: true,
value: selection,
icon: Icon(
Icons.arrow_drop_down,
color: Colors.brown,
),
iconSize: 40,
underline: Container(
height: 1,
color: Colors.transparent,
),
onChanged: (String val) => setState(() => selection = val),
items: settingsOptions.map((option) {
return DropdownMenuItem(
value: option,
child: Text(option),
);
}).toList(),
),
),
)
),
),
这是代码的输出:
如何自定义项目的宽度、高度和添加第一幅图像中的分隔符?谢谢这是一个示例,您可以随意修改
DropdownButton(
isExpanded: true,
isDense: true,
value: selection,
icon: Icon(
Icons.arrow_drop_down,
color: Colors.brown,
),
iconSize: 40,
underline: Container(
height: 1,
color: Colors.transparent,
),
onChanged: (String val) => setState(() => selection = val),
items: sampleList.map((option) {
return DropdownMenuItem(
value: option,
child: Container(
width:double.infinity,
alignment:Alignment.centerLeft,
padding: const EdgeInsets.fromLTRB(0,8.0,0,6.0),
child:Text(option),
decoration:BoxDecoration(
border:Border(top:BorderSide(color:Colors.grey,width:1))
)
),
);
}).toList(),
selectedItemBuilder:(con){
return sampleList.map((m){
return Text(m,);
}).toList();
}
)
这是一个示例,您可以随意修改
DropdownButton(
isExpanded: true,
isDense: true,
value: selection,
icon: Icon(
Icons.arrow_drop_down,
color: Colors.brown,
),
iconSize: 40,
underline: Container(
height: 1,
color: Colors.transparent,
),
onChanged: (String val) => setState(() => selection = val),
items: sampleList.map((option) {
return DropdownMenuItem(
value: option,
child: Container(
width:double.infinity,
alignment:Alignment.centerLeft,
padding: const EdgeInsets.fromLTRB(0,8.0,0,6.0),
child:Text(option),
decoration:BoxDecoration(
border:Border(top:BorderSide(color:Colors.grey,width:1))
)
),
);
}).toList(),
selectedItemBuilder:(con){
return sampleList.map((m){
return Text(m,);
}).toList();
}
)
我在pub.dev中发现了一个名为dropdown_的颤振库。它还提供了其他方法,允许您将dropdownMenuItem自定义为首选布局
DropdownBelow(
value: category,
// isDense: true,
itemWidth: MediaQuery.of(context).size.width * 0.92,
itemTextstyle: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
color: Colors.black),
boxTextstyle: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
color: Colors.grey.shade600),
boxPadding: EdgeInsets.fromLTRB(13, 12, 0, 12),
boxWidth: MediaQuery.of(context).size.width,
boxHeight: 60,
hint: Text('choose video'),
items: video.data.value.videos
.map((e) => DropdownMenuItem(
onTap: () => e.title,
value: e.title ?? category,
child: Container(
width: double.infinity,
decoration: BoxDecoration(),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
e.title ?? '$category',
overflow: TextOverflow.ellipsis,
),
),
),
))
.toList(),
onChanged: (video) {
context.read(videoProvider).cateroryOnChange(video);
},
),
库链接:我在pub.dev中发现了一个名为dropdown_的颤振库。它还提供了其他方法,允许您将dropdownMenuItem自定义为首选布局
DropdownBelow(
value: category,
// isDense: true,
itemWidth: MediaQuery.of(context).size.width * 0.92,
itemTextstyle: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
color: Colors.black),
boxTextstyle: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
color: Colors.grey.shade600),
boxPadding: EdgeInsets.fromLTRB(13, 12, 0, 12),
boxWidth: MediaQuery.of(context).size.width,
boxHeight: 60,
hint: Text('choose video'),
items: video.data.value.videos
.map((e) => DropdownMenuItem(
onTap: () => e.title,
value: e.title ?? category,
child: Container(
width: double.infinity,
decoration: BoxDecoration(),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
e.title ?? '$category',
overflow: TextOverflow.ellipsis,
),
),
),
))
.toList(),
onChanged: (video) {
context.read(videoProvider).cateroryOnChange(video);
},
),
库链接:由于DROPPUMNEUTION的容器子具有双无限宽度,
RealDox错误,但一旦注释出来,第一个Opple在AppBar下有条目高度,但随后它又默认为第二个图像。我已经接受了您的答案,但是请考虑更新它。非常感谢。我用dartpad测试过了!你能不能加上发生错误的确切位置!?我删除了容器宽度,它可以工作,但与图像中的不同。任何帮助都将不胜感激。是的,由于DropdownMenuItem的容器子对象的宽度为double.infinity,因此没有任何更改Derbox错误,但一旦注释掉,第一个打开的项目在appbar下具有项目高度,但随后默认返回到第二个有问题的图像。我已接受您的答案,但是请考虑更新它,先生。非常感谢。我用dartpad测试过了!你能不能加上发生错误的确切位置!?我删除了容器宽度,它可以工作,但与图像中的不同。任何帮助都将不胜感激。是的,没有任何更改提供链接和一些示例代码将非常有用。我已经更新了我的答案谢谢先生。我会在提供链接和一些示例代码的情况下检查它。我已经更新了我的答案。谢谢先生。我去看看