当我点击 PopupMenuButton 时,PopupMenuItems 没有显示
When i click on PopupMenuButton, PopupMenuItems are not Showing
我正在制作一个 flutter 应用程序,我有一个 PopupMenuButton
.
当我点击 PopupMenuButton
时,PopupMenuItems
没有显示。这是代码:
class _ConfigurationListScreenState extends State < ConfigurationListScreen > {
// const ConfigurationListScreen({Key? key}) : super(key: key);
dynamic dataJson;
@override
Widget build(BuildContext context) {
var configurationListChannel =
ConfigurationListChannel(downlink: updateListData);
configurationListChannel.requestConfigurationList();
final GlobalKey _menuKey = GlobalKey();
final button = PopupMenuButton(
key: _menuKey,
itemBuilder: (_) => < PopupMenuItem < String >> [
PopupMenuItem < String > (child: Text('Doge'), value: 'Doge'),
PopupMenuItem < String > (child: Text('Lion'), value: 'Lion'),
],
onSelected: (_) {});
return Scaffold(
appBar: AppBar(
title: Text("Configuration List"),
),
body: Container(
padding: EdgeInsets.all(20),
child: ListView.builder(
itemCount: dataJson.length,
itemBuilder: (BuildContext context, int index) {
return Card(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
padding: EdgeInsets.all(16),
child: Column(
children: [
updateText('device_id', index),
updateText('downlink_value', index),
updateText('downlink_desc', index),
updateText('downlink_status', index),
updateText('sent_date', index),
updateText('created_date', index),
],
),
),
Container(height: 150, color: Colors.red, child: button, alignment: Alignment.topRight, )
],
));
},
),
),
);
}
你好 Sagar 我认为你的 PopUpMenuItem 没有指定子项,所以它是不可见的。只需尝试将图标添加到它的子项。
final button = PopupMenuButton(
key: _menuKey,
child: Icon(Icons.menu)
itemBuilder: (_) => < PopupMenuItem < String >> [
PopupMenuItem < String > (child: Text('Doge'), value: 'Doge'),
PopupMenuItem < String > (child: Text('Lion'), value: 'Lion'),
],
onSelected: (_) {}
);
我不知道你为什么需要 GlobalKey
。但是您试图在列表中多次使用 PopupMenuButton
和相同的全局键。这导致了问题,您不能在小部件树中有重复的全局键。因此,如果没有添加全局键,菜单项可以正常工作。这是您的工作代码片段,但我删除了与问题无关的部分。
Widget build(BuildContext context) {
final button = PopupMenuButton(
itemBuilder: (_) => < PopupMenuItem < String >> [
PopupMenuItem < String > (child: Text('Doge'), value: 'Doge'),
PopupMenuItem < String > (child: Text('Lion'), value: 'Lion'),
],
onSelected: (_) {});
return Scaffold(
appBar: AppBar(
title: const Text("Configuration List"),
),
body: Container(
padding: const EdgeInsets.all(20),
child: ListView.builder(
itemCount: 3,
itemBuilder: (BuildContext context, int index) {
return Card(
child:
Container(
height: 150,
color: Colors.red,
child: button,
alignment: Alignment.topRight,
)
);
},
),
),
);
}
您介意解释一下全局密钥的用途吗?如果你需要它们,你应该为你的每个按钮准备一个唯一的按钮。不仅仅是列表中使用的一个按钮。但当您可能需要它们时,这并不常见。
我正在制作一个 flutter 应用程序,我有一个 PopupMenuButton
.
当我点击 PopupMenuButton
时,PopupMenuItems
没有显示。这是代码:
class _ConfigurationListScreenState extends State < ConfigurationListScreen > {
// const ConfigurationListScreen({Key? key}) : super(key: key);
dynamic dataJson;
@override
Widget build(BuildContext context) {
var configurationListChannel =
ConfigurationListChannel(downlink: updateListData);
configurationListChannel.requestConfigurationList();
final GlobalKey _menuKey = GlobalKey();
final button = PopupMenuButton(
key: _menuKey,
itemBuilder: (_) => < PopupMenuItem < String >> [
PopupMenuItem < String > (child: Text('Doge'), value: 'Doge'),
PopupMenuItem < String > (child: Text('Lion'), value: 'Lion'),
],
onSelected: (_) {});
return Scaffold(
appBar: AppBar(
title: Text("Configuration List"),
),
body: Container(
padding: EdgeInsets.all(20),
child: ListView.builder(
itemCount: dataJson.length,
itemBuilder: (BuildContext context, int index) {
return Card(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
padding: EdgeInsets.all(16),
child: Column(
children: [
updateText('device_id', index),
updateText('downlink_value', index),
updateText('downlink_desc', index),
updateText('downlink_status', index),
updateText('sent_date', index),
updateText('created_date', index),
],
),
),
Container(height: 150, color: Colors.red, child: button, alignment: Alignment.topRight, )
],
));
},
),
),
);
}
你好 Sagar 我认为你的 PopUpMenuItem 没有指定子项,所以它是不可见的。只需尝试将图标添加到它的子项。
final button = PopupMenuButton(
key: _menuKey,
child: Icon(Icons.menu)
itemBuilder: (_) => < PopupMenuItem < String >> [
PopupMenuItem < String > (child: Text('Doge'), value: 'Doge'),
PopupMenuItem < String > (child: Text('Lion'), value: 'Lion'),
],
onSelected: (_) {}
);
我不知道你为什么需要 GlobalKey
。但是您试图在列表中多次使用 PopupMenuButton
和相同的全局键。这导致了问题,您不能在小部件树中有重复的全局键。因此,如果没有添加全局键,菜单项可以正常工作。这是您的工作代码片段,但我删除了与问题无关的部分。
Widget build(BuildContext context) {
final button = PopupMenuButton(
itemBuilder: (_) => < PopupMenuItem < String >> [
PopupMenuItem < String > (child: Text('Doge'), value: 'Doge'),
PopupMenuItem < String > (child: Text('Lion'), value: 'Lion'),
],
onSelected: (_) {});
return Scaffold(
appBar: AppBar(
title: const Text("Configuration List"),
),
body: Container(
padding: const EdgeInsets.all(20),
child: ListView.builder(
itemCount: 3,
itemBuilder: (BuildContext context, int index) {
return Card(
child:
Container(
height: 150,
color: Colors.red,
child: button,
alignment: Alignment.topRight,
)
);
},
),
),
);
}
您介意解释一下全局密钥的用途吗?如果你需要它们,你应该为你的每个按钮准备一个唯一的按钮。不仅仅是列表中使用的一个按钮。但当您可能需要它们时,这并不常见。