Flutter多级抽屉导航插件multilevel_drawer的使用

发布于 1周前 作者 itying888 来自 Flutter

Flutter多级抽屉导航插件multilevel_drawer的使用

MultiLevel Drawer

MultiLevel Drawer 是一个易于实现的Flutter应用程序多级抽屉组件。只需将其用作常规Scaffold抽屉,即可轻松上手。

当前特性

  • 为选定项添加多级抽屉
  • 根据应用程序需求更改主菜单和子菜单的颜色

Demo

Demo

使用方法

要使用 MultiLevelDrawer,只需将其添加到 Scaffolddrawer 属性中:

child: Scaffold(
  drawer: MultiLevelDrawer(
    backgroundColor: Colors.white,
    rippleColor: Colors.white,
    subMenuBackgroundColor: Colors.grey.shade100,
    header: Container(                  // 抽屉头部
      height: size.height * 0.25,
      child: Center(child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Image.asset("assets/dp_default.png",width: 100,height: 100,),
          SizedBox(height: 10,),
          Text("RetroPortal Studio")
        ],
      )),
    ),
    children: [           // 每个抽屉项的子元素
      MLMenuItem(
          leading: Icon(Icons.person),
          trailing: Icon(Icons.arrow_right),
          content: Text(
            "My Profile",
          ),
          subMenuItems: [
            MLSubmenu(onClick: () {}, submenuContent: Text("Option 1")),
            MLSubmenu(onClick: () {}, submenuContent: Text("Option 2")),
            MLSubmenu(onClick: () {}, submenuContent: Text("Option 3")),
          ],
          onClick: () {}),
      MLMenuItem(
          leading: Icon(Icons.settings),
          trailing: Icon(Icons.arrow_right),
          content: Text("Settings"),
          onClick: () {},
          subMenuItems: [
            MLSubmenu(onClick: () {}, submenuContent: Text("Option 1")),
            MLSubmenu(onClick: () {}, submenuContent: Text("Option 2"))
          ]),
      MLMenuItem(
        leading: Icon(Icons.notifications),
        content: Text("Notifications"),
        onClick: () {},
      ),
      MLMenuItem(
          leading: Icon(Icons.payment),
          trailing: Icon(Icons.arrow_right),
          content: Text(
            "Payments",
          ),
          subMenuItems: [
            MLSubmenu(onClick: () {}, submenuContent: Text("Option 1")),
            MLSubmenu(onClick: () {}, submenuContent: Text("Option 2")),
            MLSubmenu(onClick: () {}, submenuContent: Text("Option 3")),
            MLSubmenu(onClick: () {}, submenuContent: Text("Option 4")),
          ],
          onClick: () {}),
    ],
  ),
  appBar: AppBar(
    backgroundColor: Colors.white,
    iconTheme: IconThemeData(color: Colors.black),
    title: Text(
      "Multi Level Menu",
      style: TextStyle(color: Colors.black),
    ),
  ),
  body: Container(
      decoration: BoxDecoration(
        gradient:
        LinearGradient(begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [
          Color.fromRGBO(255, 65, 108, 1.0),
          Color.fromRGBO(255, 75, 43, 1.0),
        ]),
      ),
      child: Center()),
),

重要类:

  • MLMenuItem:用于每个直接子项
  • MLSubmenu:用于向 MLMenuItem 添加子菜单

示例代码

以下是一个完整的示例代码,演示了如何在Flutter项目中使用 MultiLevelDrawer 插件:

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter ML Menu',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return SafeArea(
      child: Scaffold(
        drawer: MultiLevelDrawer(
          backgroundColor: Colors.white,
          rippleColor: Colors.white,
          subMenuBackgroundColor: Colors.grey.shade100,
          divisionColor: Colors.grey,
          header: Container(
            height: size.height * 0.25,
            child: Center(
                child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Image.asset(
                  "assets/dp_default.png",
                  width: 100,
                  height: 100,
                ),
                SizedBox(
                  height: 10,
                ),
                Text("RetroPortal Studio")
              ],
            )),
          ),
          children: [
            MLMenuItem(
                leading: Icon(Icons.person),
                trailing: Icon(Icons.arrow_right),
                content: Text(
                  "My Profile",
                ),
                subMenuItems: [
                  MLSubmenu(
                      onClick: () {
                        Navigator.of(context).pop();
                        Navigator.of(context).push(MaterialPageRoute(builder: (context) => SecondScreen()));
                      },
                      submenuContent: Text("Option 1")),
                  MLSubmenu(onClick: () {}, submenuContent: Text("Option 2")),
                  MLSubmenu(onClick: () {}, submenuContent: Text("Option 3")),
                  MLSubmenu(onClick: () {}, submenuContent: Text("Option 4")),
                  MLSubmenu(onClick: () {}, submenuContent: Text("Option 5")),
                  MLSubmenu(onClick: () {}, submenuContent: Text("Option 6")),
                ],
                onClick: () {}),
            MLMenuItem(
                leading: Icon(Icons.settings),
                trailing: Icon(Icons.arrow_right),
                content: Text("Settings"),
                onClick: () {},
                subMenuItems: [MLSubmenu(onClick: () {}, submenuContent: Text("Option 1")), MLSubmenu(onClick: () {}, submenuContent: Text("Option 2"))]),
            MLMenuItem(
              leading: Icon(Icons.notifications),
              content: Text("Notifications"),
              onClick: () {
                Navigator.of(context).push(MaterialPageRoute(builder: (context) => SecondScreen()));
              },
            ),
            MLMenuItem(
                leading: Icon(Icons.payment),
                trailing: Icon(Icons.arrow_right),
                content: Text(
                  "Payments",
                ),
                subMenuItems: [
                  MLSubmenu(onClick: () {}, submenuContent: Text("Option 1")),
                  MLSubmenu(onClick: () {}, submenuContent: Text("Option 2")),
                  MLSubmenu(onClick: () {}, submenuContent: Text("Option 3")),
                  MLSubmenu(onClick: () {}, submenuContent: Text("Option 4")),
                ],
                onClick: () {}),
          ],
        ),
        appBar: AppBar(
          backgroundColor: Colors.white,
          iconTheme: IconThemeData(color: Colors.black),
          title: Text(
            "Multi Level Menu",
            style: TextStyle(color: Colors.black),
          ),
        ),
        body: Container(
            decoration: BoxDecoration(
              gradient: LinearGradient(begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [
                Color.fromRGBO(255, 65, 108, 1.0),
                Color.fromRGBO(255, 75, 43, 1.0),
              ]),
            ),
            child: Center()),
      ),
    );
  }
}

路线图

计划添加更多自定义选项。

许可证

MIT许可证,详情请参见MIT License


更多关于Flutter多级抽屉导航插件multilevel_drawer的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter多级抽屉导航插件multilevel_drawer的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是一个关于如何在Flutter中使用multilevel_drawer插件来实现多级抽屉导航的示例代码。multilevel_drawer是一个用于创建多级抽屉导航的Flutter插件,可以帮助你构建复杂的导航结构。

首先,你需要在你的pubspec.yaml文件中添加multilevel_drawer依赖:

dependencies:
  flutter:
    sdk: flutter
  multilevel_drawer: ^最新版本号  # 请替换为实际可用的最新版本号

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

接下来,是一个完整的示例代码,展示如何使用multilevel_drawer

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Multi-Level Drawer Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      appBar: AppBar(
        title: Text('Multi-Level Drawer Demo'),
      ),
      drawer: MultiLevelDrawer(
        header: DrawerHeader(
          decoration: BoxDecoration(
            color: Colors.blue,
          ),
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Text(
              'Drawer Header',
              style: TextStyle(color: Colors.white),
            ),
          ),
        ),
        drawerItems: [
          MultiLevelDrawerItem(
            title: 'Home',
            icon: Icons.home,
            onTap: () {
              Navigator.pop(context); // 关闭抽屉
            },
          ),
          MultiLevelDrawerItem(
            title: 'About',
            icon: Icons.info,
            onTap: () {
              Navigator.pop(context); // 关闭抽屉
              // 可以在这里添加其他逻辑,比如跳转到About页面
            },
          ),
          MultiLevelDrawerItem(
            title: 'Services',
            icon: Icons.settings,
            subItems: [
              MultiLevelDrawerSubItem(
                title: 'Service 1',
                icon: Icons.directions_car,
                onTap: () {
                  Navigator.pop(context); // 关闭抽屉
                  // 可以在这里添加其他逻辑,比如跳转到Service 1页面
                },
              ),
              MultiLevelDrawerSubItem(
                title: 'Service 2',
                icon: Icons.directions_transit,
                onTap: () {
                  Navigator.pop(context); // 关闭抽屉
                  // 可以在这里添加其他逻辑,比如跳转到Service 2页面
                },
              ),
            ],
          ),
          // 可以继续添加更多的顶层和子项
        ],
      ),
      body: Center(
        child: Text('Home Page'),
      ),
    );
  }
}

在这个示例中,我们创建了一个简单的Flutter应用,其中包含一个多级抽屉导航。抽屉导航的头部使用了DrawerHeader,并且包含了几个顶层菜单项(MultiLevelDrawerItem)和一个包含子项的菜单项(MultiLevelDrawerItem带有subItems)。

每个菜单项和子项都定义了标题、图标和点击事件处理函数。点击事件处理函数中,我们简单地关闭了抽屉,但你可以根据需要添加跳转到其他页面的逻辑。

请注意,multilevel_drawer插件的API可能会随着版本更新而发生变化,因此请查阅最新的官方文档以获取最准确的使用方法和API参考。

回到顶部