DateFormField constructor

DateFormField({
  1. Key? key,
  2. TextEditingController? controller,
  3. String? initialValue,
  4. TextInputAction? textInputAction,
  5. TextStyle? style,
  6. StrutStyle? strutStyle,
  7. TextDirection? textDirection,
  8. TextAlign textAlign = TextAlign.start,
  9. bool autofocus = false,
  10. bool readOnly = false,
  11. bool? showCursor,
  12. bool enableSuggestions = true,
  13. MaxLengthEnforcement? maxLengthEnforcement,
  14. bool expands = false,
  15. int? maxLength,
  16. ValueChanged<String>? onChanged,
  17. GestureTapCallback? onTap,
  18. VoidCallback? onEditingComplete,
  19. ValueChanged<String>? onFieldSubmitted,
  20. FormFieldSetter<String>? onSaved,
  21. FormFieldValidator<String>? validator,
  22. bool enabled = true,
  23. double cursorWidth = 2.0,
  24. double? cursorHeight,
  25. EdgeInsets scrollPadding = const EdgeInsets.all(20.0),
  26. bool? enableInteractiveSelection,
  27. TextSelectionControls? selectionControls,
  28. Iterable<String>? autofillHints,
  29. AutovalidateMode? autovalidateMode,
  30. ScrollController? scrollController,
  31. String? restorationId,
  32. bool enableIMEPersonalizedLearning = true,
  33. FocusNode? focusNode,
  34. List<TextInputFormatter>? inputFormatters,
  35. required DateTime initialDate,
  36. required DateTime firstDate,
  37. required DateTime lastDate,
  38. DateTime? currentDate,
  39. SelectableDayPredicate? selectableDayPredicate,
  40. DatePickerMode initialCalendarMode = DatePickerMode.day,
})

Creates a DateFormField.

Implementation

DateFormField({
  super.key,
  this.controller,
  String? initialValue,
  TextInputAction? textInputAction, // TODO(as): field.
  TextStyle? style,
  StrutStyle? strutStyle,
  TextDirection? textDirection,
  TextAlign textAlign = TextAlign.start,
  bool autofocus = false,
  bool readOnly = false,
  bool? showCursor,
  bool enableSuggestions = true, // TODO(as): field.
  MaxLengthEnforcement? maxLengthEnforcement, // TODO(as): field.
  bool expands = false, // TODO(as): field.
  int? maxLength, // TODO(as): field.
  this.onChanged,
  GestureTapCallback? onTap,
  VoidCallback? onEditingComplete,
  ValueChanged<String>? onFieldSubmitted,
  super.onSaved,
  super.validator, // TODO(as): field.
  bool enabled = true,
  double cursorWidth = 2.0,
  double? cursorHeight,
  EdgeInsets scrollPadding = const EdgeInsets.all(20.0),
  bool? enableInteractiveSelection,
  TextSelectionControls? selectionControls,
  Iterable<String>? autofillHints, // TODO(as): field.
  AutovalidateMode? autovalidateMode,
  ScrollController? scrollController,
  super.restorationId,
  bool enableIMEPersonalizedLearning = true,
  this.focusNode,
  List<TextInputFormatter>? inputFormatters,

  // Used for the date picker.
  required this.initialDate,
  required this.firstDate,
  required this.lastDate,
  this.currentDate,
  this.selectableDayPredicate,
  this.initialCalendarMode = DatePickerMode.day,
  // MouseCursor? mouseCursor,
})  : assert(initialValue == null || controller == null),
      super(
        initialValue:
            controller != null ? controller.text : (initialValue ?? ''),
        enabled: enabled,
        autovalidateMode: autovalidateMode ?? AutovalidateMode.disabled,
        builder: (FormFieldState<String> field) {
          final _TextFormFieldState state = field as _TextFormFieldState;

          void onChangedHandler(String value) {
            field.didChange(value);
            if (onChanged != null) {
              onChanged(value);
            }
          }

          final FocusNode effectiveFocusNode = state._effectiveFocusNode;

          final ThemeData themeData = Theme.of(field.context);
          final ColorScheme colorScheme = themeData.colorScheme;
          final TextTheme textTheme = themeData.textTheme;

          final Widget? error = field.errorText != null
              ? Text(
                  field.errorText!,
                  style: textTheme.caption.copyWith(
                    color: textTheme.textError,
                    fontWeight: FontWeight.w500,
                  ),
                )
              : null;

          final Color background =
              enabled ? colorScheme.background[0] : colorScheme.shade[90];

          final foreground = effectiveFocusNode.hasFocus
              ? colorScheme.shade[50]
              : colorScheme.shade[30];

          final Color borderColor = field._calendarButtonActive
              ? colorScheme.background[8]
              : foreground;

          final BoxDecoration decoration = BoxDecoration(
            color: background,
            border: enabled
                ? Border.all(color: borderColor, width: _kBorderWidth)
                : null,
          );

          final List<TextInputFormatter> effectiveInputFormatters =
              inputFormatters ??
                  [
                    DesktopLocalizations.of(field.context)
                        .dateFormInputFormatter
                  ];

          return Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              DecoratedBox(
                decoration: decoration,
                child: Padding(
                  padding: decoration.padding,
                  child: Row(
                    children: [
                      Flexible(
                        child: Align(
                          alignment: Alignment.centerRight,
                          child: UnmanagedRestorationScope(
                            bucket: field.bucket,
                            child: TextField(
                              restorationId: restorationId,
                              controller: state._effectiveController,
                              focusNode: effectiveFocusNode,
                              decoration: const BoxDecoration(),
                              keyboardType: TextInputType.datetime,
                              textInputAction: textInputAction,
                              style: style,
                              strutStyle: strutStyle,
                              textAlign: textAlign,
                              textDirection: textDirection,
                              textCapitalization: TextCapitalization.none,
                              autofocus: autofocus,
                              readOnly: readOnly,
                              showCursor: showCursor,
                              autocorrect: false,
                              smartDashesType: SmartDashesType.disabled,
                              smartQuotesType: SmartQuotesType.disabled,
                              enableSuggestions: enableSuggestions,
                              maxLines: 1,
                              minLines: 1,
                              expands: expands,
                              maxLength: maxLength,
                              onChanged: onChangedHandler,
                              onTap: onTap,
                              onEditingComplete: onEditingComplete,
                              onSubmitted: onFieldSubmitted,
                              inputFormatters: effectiveInputFormatters,
                              enabled: enabled,
                              cursorWidth: cursorWidth,
                              cursorHeight: cursorHeight,
                              scrollPadding: scrollPadding,
                              enableInteractiveSelection:
                                  enableInteractiveSelection ?? !readOnly,
                              selectionControls: selectionControls,
                              autofillHints: autofillHints,
                              scrollController: scrollController,
                              enableIMEPersonalizedLearning:
                                  enableIMEPersonalizedLearning,
                              maxLengthEnforcement: maxLengthEnforcement,
                              // mouseCursor: mouseCursor,
                            ),
                          ),
                        ),
                      ),
                      Padding(
                        // Considering the padding inside [TextField].
                        padding: const EdgeInsets.only(left: 4.0, right: 8.0),
                        child: Button.icon(
                          Icons.editCalendar,
                          active: field._calendarButtonActive,
                          theme: const ButtonThemeData(itemSpacing: 0.0),
                          onPressed: field._openDatePicker,
                        ),
                      ),
                    ],
                  ),
                ),
              ),
              if (error != null)
                Padding(
                  padding: const EdgeInsets.only(top: 4.0),
                  child: Align(
                    alignment: Alignment.bottomRight,
                    child: error,
                  ),
                ),
            ],
          );
        },
      );