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

date_time_picker 封面
插件详情v2.1.0

date_time_picker

一个 Flutter 小部件,用于显示日期时间表单字段,以显示日期或时钟对话框。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
2.3万
喜欢数
375
Pub 分数
130

安装插件

flutter pub add date_time_picker

插件介绍

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

一个 Flutter 小部件,提供日期和时间表单字段,显示对话框以选择日期或时间。它支持仅日期和仅时间的选择器,跨 Android、iOS、Web、macOS、Windows 和 Linux 提供一致的用户界面。该包旨在无缝集成到表单中,通过直观的时间和日期选择提升用户体验。它利用可用的原生对话框,确保平台相关的适当行为。

适合场景

  • 表单输入项需要选择日期
  • 时间条目在调度应用程序中
  • 预订系统中的日期和时间输入
  • 用户资料设置出生日期
  • 带时间限制的事件创建

主要特点

  • 跨平台兼容性
  • 原生对话框界面
  • 对日期和时间模式的支持
  • 与 Form 小部件的轻松集成
  • 可自定义的外观和行为

适合对象

  • 需要日期/时间输入的移动和网页应用
  • 寻求简单可靠的日期选择器的开发者
  • 跨平台需要一致用户体验的应用
  • 包含大量表单且数据具有时效性的应用程序

使用建议

  • 超出基础样式的有限自定义
  • 无内置验证逻辑
  • 需要手动处理日期解析
  • 依赖于底层平台的日期/时间格式
  • 可能需要额外的本地化设置

原始 README

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

date_time_picker

pub package

Buy Me A Beer

A Flutter widget to show a text form field to display a date or clock dialog.
This widget extend TextField and has a similar behavior as TextFormField

Usage

In the pubspec.yaml of your flutter project, add the following dependency:

dependencies:
  ...
  date_time_picker: "^2.1.0"

In your library add the following import:

import 'package:date_time_picker/date_time_picker.dart';

For help getting started with Flutter, view the online documentation.

Example

There are four presentations for DateTimePicker and can be defined in the type parameter:

  • DateTimePickerType.date will present a text field with the action tap showing a datePicker dialog box;
  • DateTimePickerType.time will present a text field with the action tap showing a timePicker dialog box;
  • DateTimePickerType.dateTime will present a text field with the action tap showing a datePicker dialog box then a timePicker dialog box;
  • DateTimePickerType.dateTimeSeparated will display two text fields side by side, the first for date and the second for time. Each displaying their respective dialog box, datePicker and timePicker in the tap action;
DateTimePicker(
  type: date, // options: [date | time | dateTime | dateTimeSeparated], default is date
  ...
)

initialValue or controller.text can be null, empty or a DateTime string otherwise it will throw an error.

DateTimePicker(
  initialValue: '',
  firstDate: DateTime(2000),
  lastDate: DateTime(2100),
  dateLabelText: 'Date',
  onChanged: (val) => print(val),
  validator: (val) {
    print(val);
    return null;
  },
  onSaved: (val) => print(val),
);

More complete example:

DateTimePicker(
  type: DateTimePickerType.dateTimeSeparate,
  dateMask: 'd MMM, yyyy',
  initialValue: DateTime.now().toString(),
  firstDate: DateTime(2000),
  lastDate: DateTime(2100),
  icon: Icon(Icons.event),
  dateLabelText: 'Date',
  timeLabelText: "Hour",
  selectableDayPredicate: (date) {
    // Disable weekend days to select from the calendar
    if (date.weekday == 6 || date.weekday == 7) {
      return false;
    }

    return true;
  },
  onChanged: (val) => print(val),
  validator: (val) {
    print(val);
    return null;
  },
  onSaved: (val) => print(val),
);

The result of val in onChanged, validator and onSaved will be a DateTime String or just a Time String:

  • ex.: [2020-07-20 14:30] or [15:30] DateTimePickerType.time;
  • month, day, hour and minute will be 2 digits and time always be in 24 hours mode;
  • but the presentation in text field can be formated by the dateMask parameter.

Preview

Overview

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目