Home > Software engineering >  how to simplify dinamically this PopupMenuButton arrangement in Flutter?
how to simplify dinamically this PopupMenuButton arrangement in Flutter?

Time:11-02

I'm creating a PopupMenuButton in the AppBar() widget. Retrieving the elements in the List, dinamically,to make a PopupMenuButton.

      final List<String> entries = <String>['Choose a category','Verbs','Nouns','Adjectives','Adverbs','Determiners','Prepositions','Pronouns','Conjunctions','Expressions','Sentences' ];


 PopupMenuButton(
              itemBuilder: (context) => [
                PopupMenuItem(
                  value: 1,
                  child: Text(entries[1]),
                ),
                PopupMenuItem(
                  value: 2,
                  child: Text(entries[2]),
                ),
                PopupMenuItem(
                  value: 3,
                  child: Text(entries[3]),
                ),
                PopupMenuItem(
                  value: 4,
                  child: Text(entries[4]),
                ),
                PopupMenuItem(
                  value: 5,
                  child: Text(entries[5]),
                ),
                PopupMenuItem(
                  value: 6,
                  child: Text(entries[6]),
                ),
                PopupMenuItem(
                  value: 7,
                  child: Text(entries[7]),
                ),
                PopupMenuItem(
                  value: 8,
                  child: Text(entries[8]),
                ),
                PopupMenuItem(
                  value: 9,
                  child: Text(entries[9]),
                ),
                PopupMenuItem(
                  value: 10,
                  child: Text(entries[10]),
                ),
              ],
              onSelected: (value) {
                if (value > 0) {
                  Navigator.pushNamed(
                    context,
                    "/home",
                    arguments: {
                      'wordType': entries[value],
                      'page': "no_home",
                    },
                  );
                }
              },
            ),

I'm expecting something like this.

PopupMenuButton(
          itemBuilder: (context, index) => [
            PopupMenuItem(
              value: index,
              child: Text(entries[index]),
            ),
       onSelected: (value) {
            if (value > 0) {
              Navigator.pushNamed(
                context,
                "/home",
                arguments: {
                  'wordType': entries[value],
                  'page': "no_home",
                },
                                           ],
             ),

I have seen some solutions where an external method, even a class is needed to be created. Is there a built- in solution to do so?

thank you.

CodePudding user response:

You can do something like this

 PopupMenuButton(
  itemBuilder: (context) => List.generate(
    entries.length,
    (index) => PopupMenuItem(
      value: index,
      child: Text(
        entries[index],
      ),
    ),
  ),
),
  • Related