Skip to content

Side effect when displaying menu, after creating menus dynamically. #22

Description

@roubledodemiamgasoni

Hello, congratulations on the package.

I'm making a routine to create menu items dynamically. Reading json from an api. In this situation, I need to create the children of each item with an empty list []. So that, according to the json, I return to item and add an item to the children's list.

Doing it this way, the menu is displayed, but after the 2nd sub-menu the initial position is lost, with the items overlapping the previous menu, instead appearing after the menu arrow.

Thank you for your attention.

Example of api data

ID - TITLE
00001 - MENU1
00002 - MENU 2
0000200001 - MENU 2-1
0000200002 - MENU 2.2
000020000200001 - MENU 2.2.1
00002000020000100001 - MENU 2.2.1.1
0000200003 - MENU 2.3
00003

my code

`List carregarMenu(List departamentos) {
var menu = [];
var menuDepartamento = [];
var lista = [];

var grau = -1;

for (var i = 0; i < departamentos.length; i++) {
  if (i == 0) {
    grau = departamentos[i].grau;
  }

  if (grau != departamentos[i].grau) {
    if (departamentos[i - 1].grau == 0) {
      menuDepartamento.addAll(lista);
    } else {
      var nivelPai = departamentos[i - 1]
          .nivel
          .substring(0, departamentos[i - 1].nivel.length - 5);
      if (departamentos[i - 1].grau == 0) {
        var retornoPai = menuDepartamento.indexWhere(
          (element) =>
              (element.id as MenuDepartamentoModel).nivel ==
              nivelPai.substring(0, 5),
        );

        menuDepartamento[retornoPai].children!.addAll(lista);
      } else if (departamentos[i - 1].grau == 1) {
        var retornoPai = menuDepartamento.indexWhere(
          (element) =>
              (element.id as MenuDepartamentoModel).nivel ==
              nivelPai.substring(0, 5),
        );

        menuDepartamento[retornoPai].children!.addAll(lista);
      } else if (departamentos[i - 1].grau == 2) {
        var retornoPai = menuDepartamento.indexWhere(
          (element) =>
              (element.id as MenuDepartamentoModel).nivel ==
              nivelPai.substring(0, 5),
        );

        var retorno1 = menuDepartamento[retornoPai].children!.indexWhere(
              (element) =>
                  (element.id as MenuDepartamentoModel).nivel ==
                  nivelPai.substring(0, 10),
            );

        menuDepartamento[retornoPai]
            .children![retorno1]
            .children!
            .addAll(lista);
      }
    }
    lista = [];
    grau = departamentos[i].grau;
  }
  lista.add(adicionarMenu(departamentos[i]));
}
menu.add(
  PlutoMenuItem(
    title: 'DEPARTAMENTOS',
    icon: Icons.home,
    children: menuDepartamento,
  ),
);


return menu;

}
`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions