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.
Add the date_field package to your project's dependencies in pubspec.yaml.
dependencies:
...
date_field: ^3.0.5
Import the package in your Dart code.
import 'package:date_field/date_field.dart';
Use the DateTimeField or DateTimeFormField widget in your code, and customize it using the available parameters.
- onSaved: a callback that is called when the form is saved.
- validator: a callback that is called to validate the value.
- initialValue: the initial value of the field.
- autovalidateMode: when to validate the field.
- enabled: whether the field is enabled or disabled.
- use24hFormat: whether to use a 24-hour format for the time picker.
- dateTextStyle: the text style for the date.
- dateFormat: the format of the date.
- firstDate: the earliest date that can be selected.
- lastDate: the latest date that can be selected.
- initialDate: the initial date that is selected.
- onDateSelected: a callback that is called when a date is selected.
- decoration: the decoration for the field.
- initialEntryMode: the initial entry mode of the date picker.
- initialDatePickerMode: the initial date picker mode.
- mode: the mode of the date and time picker.
- initialTimePickerEntryMode: the initial entry mode of the time picker.
- fieldCreator: the creator of the DateTimeField.
Here's an example usage of DateTimeFormField:
DateTimeFormField(
decoration: const InputDecoration(
hintStyle: TextStyle(color: Colors.black45),
errorStyle: TextStyle(color: Colors.redAccent),
border: OutlineInputBorder(),
suffixIcon: Icon(Icons.event_note),
labelText: 'Only time',
),
mode: DateTimeFieldPickerMode.time,
autovalidateMode: AutovalidateMode.always,
validator: (e) => (e?.day ?? 0) == 1 ? 'Please not the first day' : null,
onDateSelected: (DateTime value) {
print(value);
},
)
You can find more examples and a complete example on the GitHub repository. We hope this package is helpful to you!
This package is released under the MIT license.
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!