Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
roubledodemiamgasoni opened this issue Oct 22, 2023 · 1 comment

Comments

@roubledodemiamgasoni
Copy link

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;

}
`

@roubledodemiamgasoni
Copy link
Author

If the menu items are created as in the examples, there is no problem. Only happens when I create dynamically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant