一個 Flutter 套件,為 Android、iOS、Web、macOS、Windows 和 Linux 提供一致的材質風格選擇對話框。它透過統一的 API 簡化了類原生日期、時間和自訂選擇器的整合。此套件在維持材質設計原則的同時,確保跨平台的視覺和諧。開發者可輕鬆實作日期、時間和自訂選項的選擇器,無需撰寫冗餘程式碼。它支援淺色與深色主題,並在適用情況下適應平台特定行為。
適用情境
在表單中選擇日期
選擇預約時間
自定義選擇器 UI
使用一致的樣式構建表單輸入
建立跨平台的日期/時間選擇器
主要特色
符合 Material Design
跨平台支援
輕鬆整合
主題感知渲染
可自定義的選取器內容
跨平台一致的使用者體驗
適合對象
表單建構器
日期/時間選擇功能
跨平台應用
UI一致性專案
尋求簡單選擇器解決方案的開發者
使用建議
需要 Material 應用上下文
超出主題之外的自訂功能有限
可能需要額外的樣式來處理複雜用例
不適合非 Material 設計系統
平台特定的行為可能會有略微差異
原始 README
以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。
Flutter Material Pickers
A flutter package containing commonly used material design picker dialogs. Some are new, some wrap existing or built in pickers with a common dialog and access function.
It includes:
New Pickers
showMaterialScrollPicker:
Allows selection of a string via a slot machine carousel
showMaterialNumberPicker:
Allows selection of a number via a slot machine carousel
showMaterialRadioPicker:
Allows selection of a single from a radio list
showMaterialCheckboxPicker:
Allows selection of many values from a checkbox list
showMaterialSelectionPicker:
Allows selection of a single value via an icon label list
Convenience Pickers
showMaterialDatePicker:
Allows selection of a date (uses the core date picker)
showMaterialTimePicker:
Allows selection of a time (uses the core time picker)
showMaterialColorPicker:
Allows RGB selection of a color (uses the ColorPicker of flutter_colorpicker)
showMaterialPalettePicker:
Allows Material palette selection of a color (uses the MaterialPicker of flutter_colorpicker)
showMaterialSwatchPicker:
Allows selection of a color from swatches (uses the BlockPicker of flutter_colorpicker)
showMaterialFilePicker:
Allows selection of a file using the device's default picker
Dialog
showMaterialResponsiveDialog:
Extends Dialog by making it responsive to screen orientation changes
All helpers implement an onChange handler to return picked option(s).
All helpers return Future<T> with the picked option(s).
There are some breaking changes in 3.0.0 (from 2.1.1).
Pickers accept generic types passed to pickers and helpers.
There's no need to pass several lists (items, values, icons), just pass
one list of generic type objects.
A selected item of generic type is passed as selectedItem not
selectedValue to match name of items.
To convert an item to a string displayed to a user either override
toString() in generic type class or pass transformer callback which
accepts an item and returns a String. (see examples)
To provide item's icon in SelectionPicker (and alikes), pass iconizer
callback which accepts an item and returns an Icon. (see example)
Example Usage
Empty Dialog Example
Although not a picker, per se, the showMaterialEmptyPicker helper displays the universal material design dialog wrapper that the pickers appear in. Using this directly, however, allows any content to be injected into the content area by passing in a custon Widget as the child. This code shows the basic structure of all the helpers:
If you don't already have or want to build a custom object to hold selction items, this library includes a general purpose one called PickerModel that you would use like this:
It is highly recommended you use Flutter's built in theme styling with primarySwatch to automatically style all controls across your entire application.
ThemeData(
primarySwatch: Colors.indigo,
)
If you desire to override the color for a given control, here is how to customize the theme:
var theme = ThemeData();
theme = theme.copyWith(
primaryColor: Colors.green, // background color of the header area
accentColor: Colors.black, // color of selected controls and button bar text
dialogBackgroundColor: Colors.green[300], // background color of the entire dialog
primaryTextTheme: theme.primaryTextTheme.copyWith(
title: theme.primaryTextTheme.title.copyWith(
color: Colors.lightGreen[50], // text color of the header area
),
),
textTheme: theme.textTheme.copyWith(
body1: theme.textTheme.body1.copyWith(
color: Colors.green[900], // text color of dialog text
),
button: theme.textTheme.button.copyWith(
color: Colors.lightGreen[50], // text color of the action bar buttons
),
),
);
The example app demonstrates switching between light and dark themes globally.
However, if for some reason you want to change colors in an individual dialog, several parameters are exposed to allow this:
showMaterialResponsiveDialog(
context: context,
headerColor: Colors.green, // background color of the header area
headerTextColor: Colors.white, // text fcolor of the header
backgroundColor: Colors.lightGreen, // background color of the entire dialog
buttonTextColor: Colors.red, // text color of the action bar buttons
child: Text('Custom dialog colors'),
);
Customization
You can customize the text that appears in various areas of the screen. The button labels automatically localize to the native language versions of "Ok" and "Cancel", unless replacement text is provided.
showMaterialNumberPicker(
title: 'Pick Your Age',
confirmText: 'Count me in',
cancelText: 'Negatory',
);
Sizing
To prevent dialogs from growing to full screen on larger devices (or web) two properties control the maxmium size that it will grow:
maxLongSide: 600,
maxShortSide: 400,
The sides relate to if the dialog is showing in landscape or portrait mode. If you wish larger (or smaller) dialogs you can overide these values.
Supported Platforms
[x] Android
[x] iOS
[x] Web
Dependencies
This widget set relies on these external third-party components:
Please see the Changelog page to know what's recently changed.
Contributions
If you find a bug or want a feature, but don't know how to fix/implement it, please fill an issue.
If you fixed a bug or implemented a new feature, please send a pull request. Please include a note about your change in CHANGELOG.md with your pull request.