Reputation: 21
i have a List of List of one type,
class MenuModel {
String title = "";
String? route;
List<MenuModel>? menues;
MenuModel(this.title, this.route, this.menues);
}
List<MenuModel> menu = [
MenuModel("Clientes", null, [
MenuModel("Consulta", ClientsInquiry.route, null),
MenuModel("Cuenta Corriente", ClientsCheckingAccount.route, [
MenuModel("Option 1", "route", null),
MenuModel("Option 2", "route", [
MenuModel("Option 1", "route", null),
MenuModel("Option 1", "route", null),
]),
MenuModel("Option 3", "route", null),
]),
MenuModel("Estado de Cuenta", ClientsAccountStatus.route, null),
MenuModel("Documentos", ClientsDocuments.route, null),
]),
];
];
I use this struct to define a Menu.
Where Title 1 contains Option 1 and Sub Menu 1 and Sub Menu 1 contains Option 1, Option 2 and Sub Sub Menu 1 and Sub Sub Menu contains Option 1.
Title 1 is a ExpansionTile and Option 1 is a ListTile.
I try to create a algorithm that create a Flutter code required to create this Structure.
Help please. Thanks.
Upvotes: 1
Views: 19
Reputation: 21
I solve the problem with this functions
static Widget getMenu(BuildContext context) {
List<Widget> widgetMenu = [];
for (var menu in _menu) {
widgetMenu.add(getSubOptions(context, menu));
}
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: widgetMenu,
);
}
static Widget getSubOptions(BuildContext context, MenuModel menu) {
if(menu.menues == null || menu.menues!.isEmpty){
return ListTile(
title: Text(menu.title),
onTap: () => Navigator.pushNamed(context, menu.route!),
);
} else {
List<Widget> subOptions = [];
for (var element in menu.menues!) {
subOptions.add(getSubOptions(context, element));
}
return ExpansionTile(
title: Text(menu.title),
children: subOptions,
);
}
}
Upvotes: 1