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

插件详情v7.0.0

date_field

一个表单字段小部件,使用自适应的 Material 和 Cupertino 日期时间选择器来选择 DateTime 值。它还提供了全局函数来提供选择器。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
2150
喜欢数
139
Pub 分数
160

安装插件

flutter pub add date_field

插件介绍

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

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! 📅

pub 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
Date field picker package gif on an iOS device Date field picker package gif on an Android device

Getting Started 🚀

  1. Add date_field package to your dependencies in pubspec.yaml.

    dependencies:
      ...
      date_field: ^6.0.0
    
  2. Run flutter pub get to install the package.

  3. 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:

    import 'package:intl/intl_standalone.dart' if (dart.library.html) 'package:intl/intl_browser.dart';
    
    Future<void> main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await findSystemLocale();
      runApp(const MyApp());
    }
    
  4. Import the package in your Dart code.

    import 'package:date_field/date_field.dart';
    
  5. Use the Widget. Example:

    DateTimeFormField(
      decoration: const InputDecoration(
        labelText: 'Enter Date',
      ),
      firstDate: DateTime.now().add(const Duration(days: 10)),
      lastDate: DateTime.now().add(const Duration(days: 40)),
      initialPickerDateTime: DateTime.now().add(const Duration(days: 20)),
      onChanged: (DateTime? value) {
        selectedDate = value;
      },
    ),
    
  6. To display the adaptive dialog without the field widget, you can directly use the showAdaptiveDateTimePicker.

     final DateTime? result = await showAdaptiveDateTimePicker(
       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.

Thank you for your contributions!

Maintainers 👷

所属分类

依赖

依赖清单 3 项

源码仓库

使用此插件的项目