Flutter嵌套菜单栏插件nested_menu_bar的使用

Flutter嵌套菜单栏插件nested_menu_bar的使用

嵌套菜单栏(Nested Menu Bar)是一个用于Flutter的水平菜单栏。基于PlutoMenuBar

特性

  • 多级子菜单:你可以在一个菜单中添加多个子菜单。你可以根据需要添加任意数量的子菜单。
  • 可定制化:你可以根据需要自定义菜单栏。你也可以自定义子菜单弹出框和子菜单项。
  • 复选框、单选按钮、图标、文本:你可以在菜单项中添加复选框、单选按钮、图标和文本。
  • 不同类型的菜单:你可以通过不同的onTaponHover打开子菜单。
  • 易于使用:你可以轻松地在项目中使用该菜单栏。

开始使用

从pub.dev获取包,将以下内容添加到你的pubspec.yaml文件中:

dependencies:
  nested_menu_bar: ^1.0.0

使用示例

以下是一个完整的示例代码,展示了如何在Flutter应用中使用嵌套菜单栏插件。

import 'package:flutter/material.dart';
import 'package:nested_menu_bar/nested_menu_bar.dart';

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  late final List<NestedMenuItem> hoverMenu;

  @override
  void initState() {
    super.initState();
    hoverMenu = _makeMenus(context);
  }

  void message(BuildContext context, String text) {
    ScaffoldMessenger.of(context).hideCurrentSnackBar();

    final snackBar = SnackBar(
      content: Text(text),
    );

    ScaffoldMessenger.of(context).showSnackBar(snackBar);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: NestedMenuBar(
          menus: hoverMenu,
          popUpMenuItemBorderRadius: 8,
          menuBarDecoration: BoxDecoration(
            color: Colors.black,
            boxShadow: [
              BoxShadow(
                color: Colors.black.withOpacity(0.5),
                spreadRadius: 5,
                blurRadius: 7,
                offset: const Offset(0, 3),
              ),
            ],
          ),
          menuBarItemHoverColor: Colors.white,
          menuBarItemColor: Colors.grey,
          popUpDecoration: BoxDecoration(
            color: Colors.black,
            border: Border.all(color: Colors.grey, width: 2),
            borderRadius: BorderRadius.circular(8),
          ),
          popUpPadding: 10,
          popUpMenuItemHoverForegroundColor: Colors.white,
          popUpMenuItemForegroundColor: Colors.grey,
          popUpMenuItemBackgroundColor: Colors.black,
          popUpMenuItemHoverBackgroundColor: Colors.grey,
        ),
      ),
    );
  }

  List<NestedMenuItem> _makeMenus(BuildContext context) {
    return [
      NestedMenuItem(
        title: "Home",
        icon: Icons.home,
        onTap: () => message(context, "Home"),
      ),
      NestedMenuItem(
        title: "Services",
        children: [
          NestedMenuItem(title: "DevOps", onTap: () => message(context, "DevOps")),
          NestedMenuItem(title: "Database modernization", onTap: () => message(context, "Database modernization")),
          NestedMenuItem(title: "Windows migration", onTap: () => message(context, "Windows migration")),
          NestedMenuItem(title: "Tailored cloud migration", onTap: () => message(context, "Tailored cloud migration")),
          NestedMenuItem(title: "Cloud storage solutions", onTap: () => message(context, "Cloud storage solutions")),
          NestedMenuItem(title: "DR solutions", onTap: () => message(context, "DR solutions")),
          NestedMenuItem(title: "Managed services", onTap: () => message(context, "Managed services")),
          NestedMenuItem(
            title: "App Development",
            onTap: () => message(context, "App Development"),
            children: [
              NestedMenuItem(
                title: "Mobile App Development",
                onTap: () => message(context, "Mobile App Development"),
                children: [
                  NestedMenuItem(
                    title: "Native App Development",
                    onTap: () => message(context, "Native App Development"),
                    children: [
                      NestedMenuItem(title: "Android App Development", onTap: () => message(context, "Android App Development")),
                      NestedMenuItem(title: "iOS App Development", onTap: () => message(context, "iOS App Development")),
                    ],
                  ),
                  NestedMenuItem(
                    title: "Cross Platform Development",
                    onTap: () => message(context, "Cross Platform Development"),
                    children: [
                      NestedMenuItem(title: "Flutter App Development", onTap: () => message(context, "Flutter App Development")),
                      NestedMenuItem(title: "React Native App Development", onTap: () => message(context, "React Native App Development")),
                    ],
                  ),
                ],
              ),
              NestedMenuItem(title: "Web App Development", onTap: () => message(context, "Web App Development")),
            ],
          ),
        ],
      ),
      NestedMenuItem(title: "About Us"),
    ];
  }
}

截图

Example

其他信息

  • 如需编辑包代码的帮助,请查看文档

许可证

该项目受MIT许可证保护。详细信息请参阅LICENSE文件。


更多关于Flutter嵌套菜单栏插件nested_menu_bar的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter嵌套菜单栏插件nested_menu_bar的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何在Flutter中使用nested_menu_bar插件的示例代码。这个插件允许你创建一个嵌套的菜单栏,非常适合需要多级菜单的应用程序。

首先,确保你已经在pubspec.yaml文件中添加了nested_menu_bar依赖:

dependencies:
  flutter:
    sdk: flutter
  nested_menu_bar: ^最新版本号  # 请替换为实际发布的最新版本号

然后,运行flutter pub get来安装依赖。

接下来,这里有一个简单的示例代码,展示了如何使用nested_menu_bar插件:

import 'package:flutter/material.dart';
import 'package:nested_menu_bar/nested_menu_bar.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Nested Menu Bar Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('Nested Menu Bar Example'),
        ),
        body: NestedMenuBarExample(),
      ),
    );
  }
}

class NestedMenuBarExample extends StatefulWidget {
  @override
  _NestedMenuBarExampleState createState() => _NestedMenuBarExampleState();
}

class _NestedMenuBarExampleState extends State<NestedMenuBarExample> {
  @override
  Widget build(BuildContext context) {
    return NestedMenuBar(
      menuItems: [
        NestedMenuItem(
          title: 'File',
          icon: Icons.menu,
          children: [
            NestedMenuItem(
              title: 'Open',
              icon: Icons.open_in_new,
              onTap: () {
                ScaffoldMessenger.of(context).showSnackBar(
                  SnackBar(content: Text('Open selected')),
                );
              },
            ),
            NestedMenuItem(
              title: 'Save',
              icon: Icons.save,
              onTap: () {
                ScaffoldMessenger.of(context).showSnackBar(
                  SnackBar(content: Text('Save selected')),
                );
              },
            ),
            NestedMenuItem(
              title: 'Save As...',
              icon: Icons.save_alt,
              children: [
                NestedMenuItem(
                  title: 'Text Document',
                  icon: Icons.text_fields,
                  onTap: () {
                    ScaffoldMessenger.of(context).showSnackBar(
                      SnackBar(content: Text('Save as Text Document')),
                    );
                  },
                ),
                NestedMenuItem(
                  title: 'Spreadsheet',
                  icon: Icons.table_chart,
                  onTap: () {
                    ScaffoldMessenger.of(context).showSnackBar(
                      SnackBar(content: Text('Save as Spreadsheet')),
                    );
                  },
                ),
              ],
            ),
          ],
        ),
        NestedMenuItem(
          title: 'Edit',
          icon: Icons.edit,
          children: [
            NestedMenuItem(
              title: 'Cut',
              icon: Icons.scissors_cut,
              onTap: () {
                ScaffoldMessenger.of(context).showSnackBar(
                  SnackBar(content: Text('Cut selected')),
                );
              },
            ),
            NestedMenuItem(
              title: 'Copy',
              icon: Icons.content_copy,
              onTap: () {
                ScaffoldMessenger.of(context).showSnackBar(
                  SnackBar(content: Text('Copy selected')),
                );
              },
            ),
            NestedMenuItem(
              title: 'Paste',
              icon: Icons.content_paste,
              onTap: () {
                ScaffoldMessenger.of(context).showSnackBar(
                  SnackBar(content: Text('Paste selected')),
                );
              },
            ),
          ],
        ),
        NestedMenuItem(
          title: 'Help',
          icon: Icons.help,
          onTap: () {
            ScaffoldMessenger.of(context).showSnackBar(
              SnackBar(content: Text('Help selected')),
            );
          },
        ),
      ],
    );
  }
}

在这个示例中,我们创建了一个包含多个菜单项的嵌套菜单栏。每个菜单项都可以有一个子菜单,当用户点击某个菜单项时,会显示一个SnackBar作为反馈。

这个示例展示了如何构建一个简单的多级菜单结构,并通过点击事件进行处理。你可以根据实际需求进一步自定义和扩展这个示例。

回到顶部