date_field 是一个 Flutter 包,提供了一个用于选择 DateTime 值的表单字段小部件,其选择器会根据平台自适应——Android 和 Web 使用 Material Design,iOS 使用 Cupertino。它支持所有主要平台,包括 macOS、Windows 和 Linux。该包包含全局函数,可轻松在应用程序的任何位置调用日期和时间选择器,简化了在表单和用户工作流程中的集成。它确保跨平台的一致用户体验,同时保持原生感觉。
适合场景
表单输入项需要选择日期
时间跟踪应用程序
事件调度接口
用户资料设置出生日期
基于日历的数据输入
主要特点
自适应 Material 和 Cupertino 选择器
全局选择器调用函数
跨平台支持(Android、iOS、Web、macOS、Windows、Linux)
与 Form 小部件的轻松集成
可自定义的日期/时间格式
适合对象
使用日期输入构建跨平台应用的开发人员
需要原生日期选择器的应用
包含时间相关数据的表单密集型应用
优先考虑平台特定用户界面一致性的团队
使用建议
需要 Flutter SDK 2.5 或更高版本
非英文语言环境可能需要额外的本地化设置
选择器外观的自定义选项有限,仅限基本选项
依赖于平台特定的渲染行为
原始 README
以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。
Welcome to the Date Field package! 📅
This package provides two widgets, DateTimeField and DateTimeFormField, which allow users to pick a
date and/or time from an input field. You can customize the appearance of the widgets using the
decoration argument, and specify whether to ask for a date, a time, or both using the mode
parameter. You can further customize the picker by configuring dialog properties.
iOS
Android
Getting Started 🚀
Add date_field package to your dependencies in pubspec.yaml.
dependencies:...date_field:^6.0.0
Run flutter pub get to install the package.
Initialize your systems locale. If not, users might see AM/PM even when
they configured their system to use 24h format. Add the following code to your main function:
To display the adaptive dialog without the field widget, you can directly use the showAdaptiveDateTimePicker.
finalDateTime? result = awaitshowAdaptiveDateTimePicker(
context: context,
mode: DateTimeFieldPickerMode.dateAndTime,
);
Note that you can also use the showMaterialDateTimePicker and showCupertinoDateTimePicker functions to show
the Material and Cupertino pickers directly.
Features 🎨
DateTimeField: A widget that allows users to pick a date and/or time from an input field.
DateTimeFormField: A FormField that contains a DateTimeField.
showAdaptiveDateTimePicker: A function that shows a date and/or time picker dialog based on the platform,
with the ability to customize the dialog properties.
showCupertinoDateTimePicker: A function that shows a Cupertino date and/or time picker dialog.
showMaterialDateTimePicker: A function that shows a Material date picker dialog.
Migration to 4.0.0 🚀
Version 4.0.0 contains breaking changes. Use dart fix to migrate to the latest version.
Known Issues 🐛
The pickers and formatted DateTimes adjust only on iOS and Android to the Device Systems Region.
On all other platforms, the pickers adjust to the locale.
See 32006
License 📜
This package is released under the MIT license.
Contributing 🤝
Contributions to this package are welcome! If you find a bug or have a feature request, please
create an issue on the GitHub repository. If you'd like to contribute code, please create a pull
request with your changes.
Before submitting a pull request, please make sure to run the tests and ensure they all pass.
Additionally, please follow the existing coding style and make sure your code is well-documented.