Home > Net >  When i click on PopupMenuButton, PopupMenuItems are not Showing
When i click on PopupMenuButton, PopupMenuItems are not Showing

Time:05-07

I am making a flutter app where I have a PopupMenuButton.

When i click on PopupMenuButton, PopupMenuItems are not Showing. Here is the code:

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, )
                  ],
                ));
            },
          ),
        ),
      );
    }

CodePudding user response:

I don't know why you needed a GlobalKey to it. But you were trying to use PopupMenuButton with the same global key several times in a list. That causes the problem, you can't have duplicate global keys in the widget tree. So the menu items work fine if no global key was added. Here's your working code snippet but, but I cut off the parts not related to the question.

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,
                   )
                );
            },
          ),
        ),
      );
    } 

would you mind explainig what global keys were for? if you need them, you should have a unique one for each of your buttons. not just one button used in a list. but it's not a common scenario when you might need them.

CodePudding user response:

Hello Sagar I think your PopUpMenuItem not specified with child, it can be unvisiable so. Just try adding Icon to it's child.

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: (_) {}
);
  • Related