搜索插件、代码仓与 APP 分类

menu_button 封面
插件详情v1.4.2+1

menu_button

一个可轻松自定义的菜单按钮小部件,用于在下拉列表中选择项目

近 30 天下载
72
喜欢数
134
Pub 分数
70

安装插件

flutter pub add menu_button

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

menu_button 包为 Flutter 提供了一个可自定义的下拉菜单按钮小部件,使用户能够轻松从列表中选择一项。它提供了一个简洁直观的界面,以紧凑的形式展示选项,非常适合空间有限的表单或设置。该小部件支持外观、行为和动画的自定义,使其能够适应各种设计需求。它可无缝集成到现有的 Flutter 应用中,并在无需复杂实现的情况下增强用户交互。

适合场景

  • 带下拉选择的表单输入
  • 设置面板与选项选择
  • 可自定义的导航菜单
  • 移动友好的下拉界面
  • 简化数据录入工作流程

主要特点

  • 高度可定制的外观
  • 平滑过渡动画
  • 与 Flutter UI 的轻松集成
  • 支持自定义物品构建器
  • 可访问且响应式设计

适合对象

  • 开发交互式表单的开发者
  • 寻求灵活下拉组件的设计人员
  • 需要轻量级菜单解决方案的应用程序
  • 优先考虑用户体验的项目
  • 使用 Flutter 开发跨平台应用的团队

使用建议

  • 有限的平台特定样式选项
  • 无内置验证支持
  • 需要手动处理选择回调
  • 未针对超长列表进行优化
  • 依赖外部依赖项以实现高级功能

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

menu_button

Pub Package License

Flutter widget to display a popup menu button very simply and easily customizable.

Resources

Installations

Add menu_button: ^1.4.2+1 in your pubspec.yaml dependencies. And import it:

import 'package:menu_button/menu_button.dart';

Usage

The widget has a lot of properties to customize it, we will see here the ones needed to get a "basic" menu button.

Firstly we have to declare a variable to keep the selected item (selectedKey) and a list that contains all the items we want to display in this menu button.

Here we will make a list of strings that we will call keys and that contains the values Low, Medium & High.

String selectedKey;

List<String> keys = <String>[
  'Low',
  'Medium',
  'High',
];

Now that we have these two elements we can start using the MenuButton<T> widget.

MenuButton<String>(
  child: normalChildButton,
  items: keys,
  itemBuilder: (String value) => Container(
   height: 40,
   alignment: Alignment.centerLeft,
   padding: const EdgeInsets.symmetric(vertical: 0.0, horizontal: 16),
   child: Text(value),
  ),
  toggledChild: Container(
    child: normalChildButton,
  ),
  onItemSelected: (String value) {
    setState(() {
      selectedKey = value;
    });
  },
  onMenuButtonToggle: (bool isToggle) {
    print(isToggle);
  },
)

And finally here is an example of the child widget used for the MenuButton above:

final Widget normalChildButton = SizedBox(
  width: 93,
  height: 40,
  child: Padding(
    padding: const EdgeInsets.only(left: 16, right: 11),
    child: Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: <Widget>[
        Flexible(
          child: Text(selectedKey, overflow: TextOverflow.ellipsis)
        ),
        const SizedBox(
          width: 12,
          height: 17,
          child: FittedBox(
            fit: BoxFit.fill,
            child: Icon(
              Icons.arrow_drop_down,
              color: Colors.grey,
            ),
          ),
        ),
      ],
    ),
  ),
);

Of course you can make your own according to your needs.

Basic Parameters

Parameter Description
child A widget to display the default button to trigger the menu button
items The list that contains all values that you want to display on the menu button
itemBuilder A widget to design each item of the menu button
onItemSelected Function triggered when an item is selected
onMenuButtonToggle Function triggered when menu button is triggered (true if displayed, false if not)
toggledChild Same as child but when the menu button is opened

More Parameters

Parameter Description
crossTheEdge By default false you can set it to true if you want the button to expand
divider A custom divider between each items
decoration A custom decoration for menu button
edgeMargin By default 0 add a custom value to prevent the button to not touch the edge, check the example edge_menu_button.dart for more information
itemBackgroundColor By default Colors.white add custom Colors to customize the background of every items
label Add a widget to display a custom label as MaterialDesign on top of the button, check label_menu_button.dart for more information
labelDecoration If you use a label you can set a custom LabelDecoration
menuButtonBackgroundColor By default Colors.white add custom Colors to customize the background of the menu button
popupHeight By default popupHeight is automatically calculated but if you need a custom height use this property
scrollPhysics By default items are not scrollable (NeverScrollableScrollPhysics), add a ScrollPhysics to enable it, for instance AlwaysScrollableScrollPhysics
showSelectedItemOnList By default true, set it to false if you don't want the selected items in the list

For a more detail example please take a look at the example folder.

Example

Menu button with 3 items:


If something is missing, feel free to open a ticket or contribute!

所属分类

源码仓库

使用此插件的项目