如何在颤振中改变弹出菜单项的背景色?
现在我只改变了popupmenuitem子元素的颜色,结果是:
代码如下:
PopupMenuButton<int>(
onSelected: (value) {
// TODO: Implement onSelect
},
offset: Offset(50, 50),
itemBuilder: (context) => [
PopupMenuItem(
value: 1,
child: Container(
height: double.infinity,
width: double.infinity,
color: Colors.greenAccent, // i use this to change the bgColor color right now
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Icon(Icons.check),
SizedBox(width: 10.0),
Text("Konfirmasi Update"),
SizedBox(width: 10.0),
],
),
),
),
我想要的是改变“konformasi update”选项的背景色,正如你在上面的图片中看到的,颜色是将白色区域留在选项之外。
如何完全更改PopupmenuItem背景色,而不在PopupmenuItem外部留下白色区域?
最佳答案
没有办法将PopupMenuButton
和PopupMenuItem
小部件从框中去掉,因为如果您检查源代码,则会有用于垂直和水平填充的硬编码值。
我修改了popup_menu.dart
文件的代码,特别是这些值:
const double _kMenuVerticalPadding = 0.0;//8.0;
const double _kMenuHorizontalPadding = 0.0;//16.0;
如果您想让它工作,请将此文件下载到您的项目:https://gist.github.com/diegoveloper/995f1e03ef225edc64e06525dc024b01
在项目中导入该文件并添加别名:
import 'your_project/my_popup_menu.dart' as mypopup;
用途:
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: mypopup.PopupMenuButton<int>(
elevation: 20,
onSelected: (value) {
// TODO: Implement onSelect
},
offset: Offset(50, 50),
itemBuilder: (context) => [
mypopup.PopupMenuItem(
value: 1,
child: Container(
height: double.infinity,
width: double.infinity,
color: Colors
.greenAccent, // i use this to change the bgColor color right now
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Icon(Icons.check),
SizedBox(width: 10.0),
Text("Konfirmasi Update"),
SizedBox(width: 10.0),
],
),
),
),
mypopup.PopupMenuItem(
value: 1,
child: Container(
height: double.infinity,
width: double.infinity,
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Icon(Icons.check),
SizedBox(width: 10.0),
Text("Revisi Update"),
SizedBox(width: 10.0),
],
),
),
),
],
),
),
);
}
结果