在这种特殊情况下,我如何制作第二个子菜单



如何在SubMenu1下制作第二个子菜单?

像这样:

主菜单(点击并折叠(

____子菜单项1(点击并折叠(

__________子菜单项2


现在我只有主菜单和一个子菜单。路线飞镖文件:

import 'file:///S:/AndroidStudioProjects/test_project/lib/menu.dart';
import 'package:font_awesome_flutter/font.dart';
import 'package:test_project/lib/SubMenu1.dart';

final List<dynamic> pages = [
MenuItem(title: "info", icon: Icons.book, items: [
SubMenuItem("SubMenu1", SubMenu1(),
),
]),
];
SubMenuItem getItemForKey(String key) {
SubMenuItem item;
List<dynamic> pag = List<dynamic>.from(pages);
pag.forEach((page) {
if (page is SubMenuItem && page.title == key) {
item = page;
} else if (page is MenuItem) {
page.items.forEach((sub) {
if (sub.title == key) item = sub;
});
}
});
return item;
}

和菜单页面dart文件:

import 'package:flutter/material.dart';
class MenuItem {
final String title;
final List<SubMenuItem> items;
final IconData icon;
MenuItem(
{Key key,
@required this.title,
this.items,
this.icon = Icons.label_important});
}
class SubMenuItem {
final String title;
final Widget page;
final IconData icon;
final String path;
SubMenuItem(this.title, this.page, {this.icon = Icons.block, this.path});
}
enum OpenMode { CODE, PREVIEW }

SubMenuItem类应该有一个递归的SubMenuItems列表。使用这种递归结构,您将把子菜单项添加到子菜单项中。


class SubMenuItem {
final String title;
final Widget page;
final IconData icon;
final String path;
final List<SubMenuItem> items;
SubMenuItem(this.title, this.page, {this.icon = Icons.block, this.path, this.items});
}

事实上,在MenuItem类内部定义它更合适。这样,您就不需要任何SubMenuItem类了。


class MenuItem {
final String title;
final List<MenuItem> items; // Here is the recursive structure!
final IconData icon;
MenuItem(
{Key key,
@required this.title,
this.items,
this.icon = Icons.label_important});
}

根据需要使用扩展标题main mneu=>submenu1=>子菜单11。此代码将对您有所帮助。

ExpansionTile(
title: Text("MAin Menu"),
children: [
ExpansionTile(
children: [
Text("Sub Menu 1"),
],
title: Text("Sub Menu 1"),
)
],
),

相关内容

最新更新