DateRangeField constructor

DateRangeField({
  1. Key? key,
  2. DateTime? firstDate,
  3. DateTime? lastDate,
  4. DateTime? currentDate,
  5. DatePickerEntryMode? initialEntryMode,
  6. String? helpText,
  7. String? cancelText,
  8. bool enabled = true,
  9. String? confirmText,
  10. String? saveText,
  11. String? errorFormatText,
  12. String? errorInvalidText,
  13. String? errorInvalidRangeText,
  14. String? fieldStartHintText,
  15. String? fieldEndHintText,
  16. String? fieldStartLabelText,
  17. String? fieldEndLabelText,
  18. double? width,
  19. EdgeInsets? margin,
  20. ValueChanged<DateTimeRange<DateTime>?>? onChanged,
  21. FormFieldSetter<DateTimeRange<DateTime>>? onSaved,
  22. FormFieldValidator<DateTimeRange<DateTime>>? validator,
  23. DateTimeRange<DateTime>? initialValue,
  24. bool autoValidate = false,
  25. DateFormat? dateFormat,
  26. InputDecoration decoration = const InputDecoration(),
})

Creates a DateRangeField which extends a FormField.

When using without a Form ancestor a GlobalKey is required.

Implementation

DateRangeField(
    {Key? key,
    this.firstDate,
    this.lastDate,
    this.currentDate,
    this.initialEntryMode,
    this.helpText,
    this.cancelText,
    this.enabled = true,
    this.confirmText,
    this.saveText,
    this.errorFormatText,
    this.errorInvalidText,
    this.errorInvalidRangeText,
    this.fieldStartHintText,
    this.fieldEndHintText,
    this.fieldStartLabelText,
    this.fieldEndLabelText,
    this.width,
    this.margin,
    ValueChanged<DateTimeRange?>? onChanged,
    FormFieldSetter<DateTimeRange>? onSaved,
    FormFieldValidator<DateTimeRange>? validator,
    this.initialValue,
    bool autoValidate = false,
    this.dateFormat,
    InputDecoration decoration = const InputDecoration()})
    : super(
          validator: validator,
          onSaved: onSaved,
          enabled: enabled,
          initialValue: initialValue,
          builder: (FormFieldState<DateTimeRange> state) {
            final DateFormat format =
                (dateFormat ?? DateFormat('MM/dd/yyyy'));
            final InputDecoration inputDecoration = decoration
                .copyWith(enabled: enabled)
                .applyDefaults(Theme.of(state.context).inputDecorationTheme);

            /// This is the dialog to select the date range.
            Future<Null> selectDateRange() async {
              DateTimeRange? picked = await showDateRangePicker(
                      context: state.context,
                      initialDateRange: initialValue,
                      firstDate: firstDate ?? DateTime.now(),
                      lastDate: lastDate ?? DateTime(DateTime.now().year + 5),
                      helpText: helpText ?? 'Select Date Range',
                      cancelText: cancelText ?? 'CANCEL',
                      confirmText: confirmText ?? 'OK',
                      saveText: saveText ?? 'SAVE',
                      errorFormatText: errorFormatText ?? 'Invalid format.',
                      errorInvalidText: errorInvalidText ?? 'Out of range.',
                      errorInvalidRangeText:
                          errorInvalidRangeText ?? 'Invalid range.',
                      fieldStartHintText: fieldStartHintText ?? 'Start Date',
                      fieldEndHintText: fieldEndHintText ?? 'End Date',
                      fieldStartLabelText: fieldStartLabelText ?? 'End Date',
                      fieldEndLabelText: fieldEndLabelText ?? 'End Date') ??
                  state.value;
              if (picked != state.value) {
                state.didChange(picked);
                onChanged?.call(picked);
              }
            }

            String hintText = decoration.hintText ?? '';
            return InkWell(
              /// This calls the dialog to select the date range.
              onTap: enabled ? selectDateRange : null,
              child: Container(
                margin: margin ?? EdgeInsets.all(15.0),
                width: width ?? MediaQuery.of(state.context).size.width,
                child: InputDecorator(
                  decoration:
                      inputDecoration.copyWith(errorText: state.errorText),
                  child: Text(
                      // This will display hintText if provided and if state.value is null
                      state.value == null
                          ? hintText
                          :

                          /// This displays the selected date range when the dialog is closed.
                          '${format.format(state.value!.start)} - ${format.format(state.value!.end)}',
                      style: (state.value == null &&
                              hintText != '' &&
                              decoration.hintStyle != null)
                          ? decoration.hintStyle
                          : TextStyle(
                              color: enabled
                                  ? null
                                  : Theme.of(state.context).disabledColor)),
                ),
              ),
            );
          });