【问题标题】:How do I make second submenu in this particular case?在这种特殊情况下如何制作第二个子菜单?
【发布时间】:2021-02-24 22:04:01
【问题描述】:

如何在 SubMenu1 下创建第二个子菜单?

像这样:

主菜单(点击并折叠)

____SubMenuItem1(点击并折叠)

__________SubMenuItem2


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

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 }

【问题讨论】:

    标签: android-studio flutter dart flutter-layout


    【解决方案1】:

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

    
    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});
    }
    
    

    【讨论】:

    • 您好,感谢您的努力。我收到错误:无法将元素类型“SubMenuItem”分配给列表类型“MenuItem”。
    【解决方案2】:

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

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

    【讨论】:

    • 是的,但你如何使用它打开路线?
    • 使用 GestureDetector 或 InkWell 包装小部件
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多