DateTimePicker constructor

DateTimePicker({
  1. Key? key,
  2. DateTimePickerType type = DateTimePickerType.date,
  3. TextEditingController? controller,
  4. DateTime? firstDate,
  5. DateTime? lastDate,
  6. DateTime? initialDate,
  7. TimeOfDay? initialTime,
  8. String? dateMask,
  9. Widget? icon,
  10. String? dateLabelText,
  11. String? timeLabelText,
  12. String? dateHintText,
  13. String? timeHintText,
  14. String? calendarTitle,
  15. String? cancelText,
  16. String? confirmText,
  17. String? fieldLabelText,
  18. String? fieldHintText,
  19. String? errorFormatText,
  20. String? errorInvalidText,
  21. DatePickerEntryMode? initialEntryMode,
  22. DatePickerMode? initialDatePickerMode,
  23. bool selectableDayPredicate(
    1. DateTime
    )?,
  24. TextDirection? textDirection,
  25. Locale? locale,
  26. bool useRootNavigator = false,
  27. RouteSettings? routeSettings,
  28. bool use24HourFormat = true,
  29. double? timeFieldWidth,
  30. bool timePickerEntryModeInput = false,
  31. String? initialValue,
  32. FocusNode? focusNode,
  33. InputDecoration? decoration,
  34. TextCapitalization textCapitalization = TextCapitalization.none,
  35. TextInputAction? textInputAction,
  36. TextStyle? style,
  37. StrutStyle? strutStyle,
  38. TextAlign textAlign = TextAlign.start,
  39. TextAlignVertical? textAlignVertical,
  40. bool autofocus = false,
  41. bool readOnly = false,
  42. ToolbarOptions? toolbarOptions,
  43. bool showCursor = false,
  44. bool obscureText = false,
  45. bool autocorrect = true,
  46. SmartDashesType? smartDashesType,
  47. SmartQuotesType? smartQuotesType,
  48. bool enableSuggestions = true,
  49. bool autovalidate = false,
  50. MaxLengthEnforcement? maxLengthEnforcement,
  51. int maxLines = 1,
  52. int? minLines,
  53. bool expands = false,
  54. int? maxLength,
  55. ValueChanged<String>? onChanged,
  56. VoidCallback? onEditingComplete,
  57. ValueChanged<String>? onFieldSubmitted,
  58. FormFieldSetter<String>? onSaved,
  59. FormFieldValidator<String>? validator,
  60. List<TextInputFormatter>? inputFormatters,
  61. bool enabled = true,
  62. double cursorWidth = 2.0,
  63. Radius? cursorRadius,
  64. Color? cursorColor,
  65. Brightness? keyboardAppearance,
  66. EdgeInsets scrollPadding = const EdgeInsets.all(20.0),
  67. bool enableInteractiveSelection = true,
  68. InputCounterWidgetBuilder? buildCounter,
  69. 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 new 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,
  ToolbarOptions? toolbarOptions,
  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,
              toolbarOptions: toolbarOptions,
              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);
          }
        },
      );