Flutter菜单列表展示插件flutter_menu_list的使用

Flutter菜单列表展示插件flutter_menu_list的使用

flutter_menu_list 是一个用于在 Flutter 应用中展示简单菜单列表的小部件。它提供了类似于 iOS 的菜单列表显示,并且支持暗色主题。


特性

  • 提供类似 iOS 的菜单列表显示。
  • 支持暗色主题。

使用方法

简单使用

以下是一个简单的示例,展示了如何使用 MenuListContainer 来创建一个菜单列表。

import 'package:flutter_menu_list/flutter_menu_list.dart';

MenuListContainer(
  sections: [
    MenuListSection(
      title: MenuListSectionTitle(title: 'First Title'),
      menus: [
        MenuListContent(
          leading: Icon(
            Icons.ac_unit,
            color: Colors.white,
            size: 20,
          ),
          title: Text('Title1'),
          onTap: () {},
        ),
        MenuListContent(
          title: Text('Title2'),
          onTap: () {},
        ),
      ],
    ),
    MenuListSection(
      title: MenuListSectionTitle(title: 'Second Title'),
      menus: [
        MenuListContent(
          title: Text('Display of very long titles'),
          subTitle: MenuListSubTitle('Display of very long sub titles'),
          onTap: () {},
        ),
        MenuListContent(
          title: Text('Title4'),
          trailing: SizedBox(
            height: 20,
            child: CupertinoSwitch(
              value: true,
              onChanged: (value) {},
            ),
          ),
        ),
      ],
    ),
    MenuListSection(
      title: MenuListSectionTitle(
        title: 'Third Title',
        padding: const EdgeInsets.only(
          left: 15,
          top: 8,
          bottom: 8,
        ),
      ),
      lineIndent: 0,
      menus: [
        MenuListContent(
          leadingSpace: 15,
          title: Text('Display of very long titles'),
          subTitle: MenuListSubTitle('Display of very long sub titles'),
          onTap: () {},
        ),
        MenuListContent(
          leadingSpace: 15,
          title: Text('Title4'),
          trailing: SizedBox(
            height: 20,
            child: CupertinoSwitch(
              value: true,
              onChanged: (value) {},
            ),
          ),
        ),
      ],
    ),
  ],
);

在列中使用

如果需要将 MenuListContainer 放置在一个列中,可以使用 Expanded 小部件来确保其占据剩余的空间。

Column(
  children: [
    Container(
      padding: const EdgeInsets.all(10),
      child: const Text(
        'Example App',
        style: TextStyle(
          fontSize: 20,
        ),
      ),
    ),
    /// Wrapping it up is important.
    const Expanded(
      child: MenuListContainer(
        sections: [
          MenuListSection(
            title: MenuListSectionTitle(title: 'Drawer Menu'),
            menus: [
              MenuListContent(
                title: Text('Title'),
              ),
            ],
          )
        ],
      ),
    ),
  ],
);

自定义主题

可以通过修改默认主题来实现自定义样式。

// 修改默认的亮色主题
final customeLightTheme = MenuListThemes.lightTheme.copyWith();

// 修改默认的暗色主题
final customeDarkTheme = MenuListThemes.darkTheme.copyWith();

// 创建自定义主题
final customizedTheme = ThemeData(
  // 背景颜色
  backgroundColor: Colors.white,
  // 菜单列表内容背景颜色
  primaryColor: Colors.grey,
  textTheme: TextTheme(
    // 文本样式
    bodyText1: TextStyle(),
    subtitle1: TextStyle(), // Android 子标题样式
    subtitle2: TextStyle(), // iOS 子标题样式
  ),
  // 图标样式
  iconTheme: IconThemeData(),
  // 分割线颜色
  dividerColor: Colors.black,
);

应用自定义主题:

MenuListContainer(
  // 设置亮色主题
  theme: customizedTheme,
  // 设置暗色主题
  darkTheme: customizedTheme,
);

完整示例代码

以下是完整的示例代码,包含菜单列表的基本使用和自定义主题的应用。

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_menu_list/flutter_menu_list.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(primarySwatch: Colors.blue),
      darkTheme: ThemeData.dark(),
      home: const Example(),
    );
  }
}

class Example extends StatelessWidget {
  const Example({Key? key}) : super(key: key);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Flutter Menu List'),
      ),
      drawer: Drawer(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Container(
              color: Theme.of(context).backgroundColor,
              padding: const EdgeInsets.all(20),
              child: const SafeArea(
                child: Center(
                  child: Text('Test'),
                ),
                bottom: false,
              ),
            ),
            const Divider(
              height: 0,
              color: Color(0xFFEEEEEE),
            ),
            Expanded(
              child: MediaQuery.removePadding(
                context: context,
                removeTop: true,
                child: const MenuListContainer(
                  sections: [
                    MenuListSection(
                      title: MenuListSectionTitle(title: 'Drawer Menu'),
                      menus: [
                        MenuListContent(
                          title: Text('Title'),
                        ),
                      ],
                    )
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
      body: SizedBox(
        height: double.infinity,
        child: MenuListContainer(
          sections: [
            MenuListSection(
              title: const MenuListSectionTitle(title: 'First Title'),
              menus: [
                MenuListContent(
                  leading: const Icon(
                    Icons.ac_unit,
                    color: Colors.white,
                    size: 20,
                  ),
                  title: const Text('Title1'),
                  onTap: () {},
                ),
                MenuListContent(
                  title: const Text('Title2'),
                  onTap: () {},
                ),
              ],
            ),
            MenuListSection(
              title: const MenuListSectionTitle(title: 'Second Title'),
              menus: [
                MenuListContent(
                  title: const Text('Display of very long titles'),
                  subTitle:
                      const MenuListSubTitle('Display of very long sub titles'),
                  onTap: () {},
                ),
                MenuListContent(
                  title: const Text('Title4'),
                  trailing: SizedBox(
                    height: 20,
                    child: CupertinoSwitch(
                      value: true,
                      onChanged: (value) {},
                    ),
                  ),
                ),
              ],
            ),
            MenuListSection(
              title: const MenuListSectionTitle(
                title: 'Third Title',
                padding: EdgeInsets.only(
                  left: 15,
                  top: 8,
                  bottom: 8,
                ),
              ),
              lineIndent: 0,
              menus: [
                MenuListContent(
                  leadingSpace: 15,
                  title: const Text('Display of very long titles'),
                  subTitle:
                      const MenuListSubTitle('Display of very long sub titles'),
                  onTap: () {},
                ),
                MenuListContent(
                  leadingSpace: 15,
                  title: const Text('Title4'),
                  trailing: SizedBox(
                    height: 20,
                    child: CupertinoSwitch(
                      value: true,
                      onChanged: (value) {},
                    ),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

更多关于Flutter菜单列表展示插件flutter_menu_list的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

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


flutter_menu_list 是一个 Flutter 插件,用于展示菜单列表。它可以帮助你快速构建一个带有菜单项的用户界面。以下是一个简单的使用示例,展示如何使用 flutter_menu_list 插件来创建一个菜单列表。

1. 添加依赖

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

dependencies:
  flutter:
    sdk: flutter
  flutter_menu_list: ^1.0.0  # 请使用最新版本

然后运行 flutter pub get 来获取依赖。

2. 导入插件

在你的 Dart 文件中导入 flutter_menu_list 插件:

import 'package:flutter_menu_list/flutter_menu_list.dart';

3. 创建菜单列表

接下来,你可以使用 MenuList 组件来创建一个菜单列表。以下是一个简单的示例:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Menu List Example'),
        ),
        body: MenuList(
          items: [
            MenuItem(
              title: 'Home',
              icon: Icons.home,
              onTap: () {
                print('Home tapped');
              },
            ),
            MenuItem(
              title: 'Settings',
              icon: Icons.settings,
              onTap: () {
                print('Settings tapped');
              },
            ),
            MenuItem(
              title: 'Profile',
              icon: Icons.person,
              onTap: () {
                print('Profile tapped');
              },
            ),
          ],
        ),
      ),
    );
  }
}

4. 运行应用

现在你可以运行你的应用,并看到类似于以下的效果:

  • 一个带有标题和图标的菜单列表。
  • 当点击某个菜单项时,控制台会输出相应的消息。

5. 自定义选项

flutter_menu_list 还提供了一些自定义选项,例如:

  • itemPadding: 设置菜单项的内边距。
  • itemMargin: 设置菜单项的外边距。
  • itemDecoration: 设置菜单项的装饰(如背景颜色、边框等)。
  • textStyle: 设置菜单项标题的文本样式。

你可以根据需要进行自定义,例如:

MenuList(
  items: [
    MenuItem(
      title: 'Home',
      icon: Icons.home,
      onTap: () {
        print('Home tapped');
      },
    ),
    // 其他菜单项
  ],
  itemPadding: EdgeInsets.all(10),
  itemMargin: EdgeInsets.symmetric(vertical: 5),
  itemDecoration: BoxDecoration(
    color: Colors.blue[50],
    borderRadius: BorderRadius.circular(5),
  ),
  textStyle: TextStyle(
    fontSize: 16,
    fontWeight: FontWeight.bold,
    color: Colors.blue,
  ),
);
回到顶部