Repository: flutter-form-builder-ecosystem/flutter_form_builder

Issue:
[FormBuilderDateTimePicker]: Got problem if initialDate is null and lastDate is years ago.
### Is there an existing issue for this?

- [X] I have searched the existing issues

### Package/Plugin version

9.2.1

### Platforms

- [ ] Android
- [X] iOS
- [ ] Linux
- [ ] MacOS
- [ ] Web
- [ ] Windows

### Flutter doctor

<details>
  <summary>Flutter doctor</summary>

<!-- Paste the output below inside ``` and ``` -->
```bash
Flutter (Channel stable, 3.16.9, on
    macOS 14.2.1 23C71 darwin-arm64, locale
    en-CA)
[✓] Xcode - develop for iOS and macOS (Xcode
    15.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2023.1)
```

</details>


### Minimal code example

<details>
  <summary>Code sample</summary>

<!-- Paste the output below inside ``` and ``` -->

```dart
FormBuilderDateTimePicker(
  name: 'birthday',
  initialValue: null,
  firstDate: DateTime(1923, 1, 1),
  lastDate: DateTime(2006, 12, 31),
),
```

</details>


### Current Behavior

Got error:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: 'package:flutter/src/material/date_picker.dart': Failed assertion: line 201 pos 5: 'initialDate == null || !initialDate.isAfter(lastDate)': initialDate 2024-02-13 00:00:00.000 must be on or before lastDate 2006-12-31 00:00:00.000.

### Expected Behavior

The code should work for a null initialDate.

### Steps To Reproduce

Add the form:

```dart
FormBuilderDateTimePicker(
  name: 'birthday',
  initialValue: null,
  firstDate: DateTime(1923, 1, 1),
  lastDate: DateTime(2006, 12, 31),
),
```

And click the field to select a date.

### Aditional information

In _showDatePicker, if there is no initialDate, it will set to DateTime.now():

```dart
  Future<DateTime?> _showDatePicker(
      BuildContext context, DateTime? currentValue) {
    return showDatePicker(
      context: context,
      selectableDayPredicate: widget.selectableDayPredicate,
      initialDatePickerMode: widget.initialDatePickerMode,
      initialDate: currentValue ?? widget.initialDate ?? DateTime.now(),
     ....
```

So, it can't pass the assert:

```dart
assert(
    initialDate == null || !initialDate.isAfter(lastDate),
    'initialDate $initialDate must be on or before lastDate $lastDate.',
  );
```


Discussion: Hi!
I think that here need to improve the assert logic.
If you want, open a PR to solve this. Thanks!
same problem, i have error when initialValue is null on FormBuilderDateTimePicker.

Fix patch:
diff --git a/lib/src/fields/form_builder_date_time_picker.dart b/lib/src/fields/form_builder_date_time_picker.dart
index f1f2d3276..eb21a3e1d 100644
--- a/lib/src/fields/form_builder_date_time_picker.dart
+++ b/lib/src/fields/form_builder_date_time_picker.dart
@@ -23,7 +23,7 @@ class FormBuilderDateTimePicker extends FormBuilderFieldDecoration<DateTime> {
   /// (Sunday, June 3, 2018 at 9:24pm)
   final DateFormat? format;
 
-  /// The date the calendar opens to when displayed. Defaults to the current date.
+  /// The date the calendar opens to when displayed. Defaults to null.
   ///
   /// To preset the widget's value, use [initialValue] instead.
   final DateTime? initialDate;
@@ -327,7 +327,7 @@ class _FormBuilderDateTimePickerState extends FormBuilderFieldDecorationState<
       context: context,
       selectableDayPredicate: widget.selectableDayPredicate,
       initialDatePickerMode: widget.initialDatePickerMode,
-      initialDate: currentValue ?? widget.initialDate ?? DateTime.now(),
+      initialDate: currentValue ?? widget.initialDate,
       firstDate: widget.firstDate ?? DateTime(1900),
       lastDate: widget.lastDate ?? DateTime(2100),
       locale: widget.locale,


Test patch:
diff --git a/test/src/fields/form_builder_date_time_picker_test.dart b/test/src/fields/form_builder_date_time_picker_test.dart
index 961de70cd..0ad9439fa 100644
--- a/test/src/fields/form_builder_date_time_picker_test.dart
+++ b/test/src/fields/form_builder_date_time_picker_test.dart
@@ -41,7 +41,9 @@ void main() {
       expect(formValue<DateTime>(widgetName),
           DateTime(dateNow.year, dateNow.month, testDay, 12));
     });
-    testWidgets('input keyboard type', (WidgetTester tester) async {
+    testWidgets(
+        'should change to text field and show keyboard when edit icon is pressed',
+        (WidgetTester tester) async {
       const widgetName = 'fdtp3';
       final widgetKey = UniqueKey();
       const keyboardType = TextInputType.datetime;
@@ -63,6 +65,30 @@ void main() {
       final textField = tester.widget<TextField>(find.byType(TextField).first);
       expect(textField.keyboardType, equals(keyboardType));
     });
+    testWidgets('should show a past year when set on lastDate',
+        (WidgetTester tester) async {
+      const widgetName = 'fdtp3';
+      final widgetKey = UniqueKey();
+      const confirmText = 'OK';
+      const cancelText = 'CANCEL';
+      final year = 2006;
+
+      final testWidget = FormBuilderDateTimePicker(
+        key: widgetKey,
+        name: widgetName,
+        confirmText: confirmText,
+        cancelText: cancelText,
+        initialDate: null,
+        lastDate: DateTime(year, 12, 31),
+      );
+      await tester.pumpWidget(buildTestableFieldWidget(testWidget));
+      await tester.tap(find.byKey(widgetKey));
+      await tester.pumpAndSettle();
+
+      expect(find.text(confirmText), findsOneWidget);
+      expect(find.text(cancelText), findsOneWidget);
+      expect(find.text('December ${year.toString()}'), findsOneWidget);
+    });
     group('initial value -', () {
       testWidgets('to FormBuilder', (WidgetTester tester) async {
         const widgetName = 'fdtp2';