搜尋套件、儲存庫與 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!

所屬分類

原始碼儲存庫

使用此套件的專案