DateTimePicker constructor
DateTimePicker({
- Key? key,
- DateTimePickerType type = DateTimePickerType.date,
- TextEditingController? controller,
- DateTime? firstDate,
- DateTime? lastDate,
- DateTime? initialDate,
- TimeOfDay? initialTime,
- String? dateMask,
- Widget? icon,
- String? dateLabelText,
- String? timeLabelText,
- String? dateHintText,
- String? timeHintText,
- String? calendarTitle,
- String? cancelText,
- String? confirmText,
- String? fieldLabelText,
- String? fieldHintText,
- String? errorFormatText,
- String? errorInvalidText,
- DatePickerEntryMode? initialEntryMode,
- DatePickerMode? initialDatePickerMode,
- bool selectableDayPredicate()?,
- TextDirection? textDirection,
- Locale? locale,
- RouteSettings? routeSettings,
- bool use24HourFormat = true,
- double? timeFieldWidth,
- bool timePickerEntryModeInput = false,
- String? initialValue,
- FocusNode? focusNode,
- InputDecoration? decoration,
- TextCapitalization textCapitalization = TextCapitalization.none,
- TextInputAction? textInputAction,
- TextStyle? style,
- StrutStyle? strutStyle,
- TextAlign textAlign = TextAlign.start,
- TextAlignVertical? textAlignVertical,
- bool autofocus = false,
- bool readOnly = false,
- Widget contextMenuBuilder()?,
- bool showCursor = false,
- bool obscureText = false,
- bool autocorrect = true,
- SmartDashesType? smartDashesType,
- SmartQuotesType? smartQuotesType,
- bool enableSuggestions = true,
- bool autovalidate = false,
- MaxLengthEnforcement? maxLengthEnforcement,
- int maxLines = 1,
- int? minLines,
- bool expands = false,
- int? maxLength,
- ValueChanged<
String> ? onChanged, - VoidCallback? onEditingComplete,
- ValueChanged<
String> ? onFieldSubmitted, - FormFieldSetter<
String> ? onSaved, - FormFieldValidator<
String> ? validator, - List<
TextInputFormatter> ? inputFormatters, - bool enabled = true,
- double cursorWidth = 2.0,
- Radius? cursorRadius,
- Color? cursorColor,
- Brightness? keyboardAppearance,
- EdgeInsets scrollPadding = const EdgeInsets.all(20.0),
- bool enableInteractiveSelection = true,
- InputCounterWidgetBuilder? buildCounter,
- ScrollPhysics? scrollPhysics,
Creates a DateTimePicker that contains a TextField.
When a controller is specified, initialValue must be null (the
default). If controller is null, then a TextEditingController
will be constructed automatically and its text will be initialized
to initialValue or the empty string.
For documentation about the various parameters, see the TextField class and TextField, the constructor.
Implementation
DateTimePicker({
Key? key,
this.type = DateTimePickerType.date,
this.controller,
this.firstDate,
this.lastDate,
this.initialDate,
this.initialTime,
this.dateMask,
this.icon,
this.dateLabelText,
this.timeLabelText,
this.dateHintText,
this.timeHintText,
this.calendarTitle,
this.cancelText,
this.confirmText,
this.fieldLabelText,
this.fieldHintText,
this.errorFormatText,
this.errorInvalidText,
this.initialEntryMode,
this.initialDatePickerMode,
this.selectableDayPredicate,
this.textDirection,
this.locale,
this.useRootNavigator = false,
this.routeSettings,
this.use24HourFormat = true,
this.timeFieldWidth,
this.timePickerEntryModeInput = false,
String? initialValue,
FocusNode? focusNode,
InputDecoration? decoration,
//TextInputType keyboardType,
TextCapitalization textCapitalization = TextCapitalization.none,
TextInputAction? textInputAction,
TextStyle? style,
StrutStyle? strutStyle,
TextAlign textAlign = TextAlign.start,
TextAlignVertical? textAlignVertical,
bool autofocus = false,
bool readOnly = false,
Widget Function(BuildContext, EditableTextState)? contextMenuBuilder,
bool showCursor = false,
bool obscureText = false,
bool autocorrect = true,
SmartDashesType? smartDashesType,
SmartQuotesType? smartQuotesType,
bool enableSuggestions = true,
bool autovalidate = false,
MaxLengthEnforcement? maxLengthEnforcement,
int maxLines = 1,
int? minLines,
bool expands = false,
int? maxLength,
this.onChanged,
VoidCallback? onEditingComplete,
ValueChanged<String>? onFieldSubmitted,
FormFieldSetter<String>? onSaved,
FormFieldValidator<String>? validator,
List<TextInputFormatter>? inputFormatters,
bool enabled = true,
double cursorWidth = 2.0,
Radius? cursorRadius,
Color? cursorColor,
Brightness? keyboardAppearance,
EdgeInsets scrollPadding = const EdgeInsets.all(20.0),
bool enableInteractiveSelection = true,
InputCounterWidgetBuilder? buildCounter,
ScrollPhysics? scrollPhysics,
}) : assert(initialValue == null || controller == null),
assert(type == DateTimePickerType.time || firstDate != null),
assert(type == DateTimePickerType.time || lastDate != null),
assert(maxLines > 0),
assert(minLines == null || minLines > 0),
assert(
(minLines == null) || (maxLines >= minLines),
"minLines can't be greater than maxLines",
),
assert(
!expands || (minLines == null),
'minLines and maxLines must be null when expands is true.',
),
assert(
!obscureText || maxLines == 1,
'Obscured fields cannot be multiline.',
),
assert(maxLength == null || maxLength > 0),
super(
key: key,
initialValue:
controller != null ? controller.text : (initialValue ?? ''),
onSaved: onSaved,
validator: validator,
autovalidateMode: autovalidate
? AutovalidateMode.always
: AutovalidateMode.disabled,
enabled: enabled,
builder: (FormFieldState<String> field) {
final state = field as _DateTimePickerState;
void onChangedHandler(String value) {
if (onChanged != null) {
onChanged(value);
}
field.didChange(value);
}
Widget buildField(DateTimePickerType peType) {
GestureTapCallback lfOnTap;
TextEditingController loCtrl;
InputDecoration loDecoration;
switch (peType) {
case DateTimePickerType.time:
lfOnTap = state._showTimePickerDialog;
loCtrl = state._timeLabelController;
loDecoration = InputDecoration(
labelText: timeLabelText,
icon: icon,
hintText: timeHintText,
);
if (type == DateTimePickerType.dateTimeSeparate) {
loDecoration = InputDecoration(
labelText: timeLabelText,
hintText: timeHintText,
);
}
break;
case DateTimePickerType.dateTime:
lfOnTap = state._showDateTimePickerDialog;
loCtrl = state._dateLabelController;
loDecoration = InputDecoration(
labelText: dateLabelText,
icon: icon,
hintText: dateHintText,
);
break;
default:
lfOnTap = state._showDatePickerDialog;
loCtrl = state._dateLabelController;
loDecoration = InputDecoration(
labelText: dateLabelText,
icon: icon,
hintText: dateHintText,
);
}
loDecoration = decoration ?? loDecoration
..applyDefaults(
Theme.of(field.context).inputDecorationTheme,
);
return TextField(
readOnly: true,
onTap: readOnly ? null : lfOnTap,
controller: loCtrl,
decoration: loDecoration.copyWith(
errorText: field.errorText,
),
focusNode: focusNode,
keyboardType: TextInputType.datetime,
textInputAction: textInputAction,
style: style,
strutStyle: strutStyle,
textAlign: textAlign,
textAlignVertical: textAlignVertical,
//textDirection: textDirection,
textCapitalization: textCapitalization,
autofocus: autofocus,
contextMenuBuilder: contextMenuBuilder,
showCursor: showCursor,
obscureText: obscureText,
autocorrect: autocorrect,
smartDashesType: smartDashesType ??
(obscureText
? SmartDashesType.disabled
: SmartDashesType.enabled),
smartQuotesType: smartQuotesType ??
(obscureText
? SmartQuotesType.disabled
: SmartQuotesType.enabled),
enableSuggestions: enableSuggestions,
maxLengthEnforcement: maxLengthEnforcement,
maxLines: maxLines,
minLines: minLines,
expands: expands,
maxLength: maxLength,
onChanged: onChangedHandler,
onEditingComplete: onEditingComplete,
onSubmitted: onFieldSubmitted,
inputFormatters: inputFormatters,
enabled: enabled,
cursorWidth: cursorWidth,
cursorRadius: cursorRadius,
cursorColor: cursorColor,
scrollPadding: scrollPadding,
scrollPhysics: scrollPhysics,
keyboardAppearance: keyboardAppearance,
enableInteractiveSelection: enableInteractiveSelection,
buildCounter: buildCounter,
);
}
switch (type) {
case DateTimePickerType.time:
return buildField(DateTimePickerType.time);
case DateTimePickerType.dateTime:
return buildField(DateTimePickerType.dateTime);
case DateTimePickerType.dateTimeSeparate:
return Row(children: <Widget>[
Expanded(child: buildField(DateTimePickerType.date)),
const SizedBox(width: 15),
SizedBox(
width: timeFieldWidth ?? 100,
child: buildField(DateTimePickerType.time),
)
]);
default:
return buildField(DateTimePickerType.date);
}
},
);