Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date Won't pop-up and blows assertion in showDatePicker #205

Closed
jamiethain opened this issue Dec 15, 2019 · 1 comment
Closed

Date Won't pop-up and blows assertion in showDatePicker #205

jamiethain opened this issue Dec 15, 2019 · 1 comment

Comments

@jamiethain
Copy link

Show Date Picker has an assertion that First Date must be less than Initial Date.

From ShowPicker

  assert(!initialDate.isBefore(firstDate), 'initialDate must be on or after firstDate');
  assert(!initialDate.isAfter(lastDate), 'initialDate must be on or before lastDate');
  assert(!firstDate.isAfter(lastDate), 'lastDate must be on or after firstDate');

If with Form Builder you set the initial date and first date to the exact same time using DateTime.now() the assertion fails and the date will not "popup"

This is a work-around where I set the date one second less.


                            FormBuilderDateTimePicker(
                              attribute: "Date",
                              onChanged: _onChanged,
                              inputType: InputType.date,
                              format: DateFormat("EEE, d-MMM-yy"),
                              **initialValue: DateTime.now(),
                              firstDate: DateTime.now().subtract(Duration(seconds: 1)),**
                              decoration: InputDecoration(labelText: "Date"),


You might want to consider something like the following (THIS IS NOT TESTED) just straight from my head.

 Future<DateTime> _showDatePicker(
      BuildContext context, DateTime currentValue) {
    DateTime _initDate = currentValue ?? widget.initialDate ?? DateTime.now(); 
    DateTime _firstDate = widget.firstDate ?? DateTime(1900);
    
    if (! _initDate.isAfter(_firstDate) )
        _initDate = _initDate.subtract(Duration(seconds: 1)); 
    
    if (widget.datePicker != null) {
      return widget.datePicker(context);
    } else {
    
      return showDatePicker(
          context: context,
          selectableDayPredicate: widget.selectableDayPredicate,
          initialDatePickerMode:
              widget.initialDatePickerMode ?? DatePickerMode.day,
          // ignore: deprecated_member_use_from_same_package
          initialDate: _initDate,
          firstDate: _firstDate
          lastDate: widget.lastDate ?? DateTime(2100));
    }
  }

For some reason I could not update and step through it, to test it. 

Some default setting probably on VSCode that I don't have time to research. 

Jamie 
@jamiethain
Copy link
Author

Ok so after I stopped an "recompiled" I could step through the code I think this works, it does work for me. You might want to run an assertion until people fix it, but at least it doesn't inadvertently act up.

I also was able to figure out why it was breaking randomly if you firstdate, before initial value in the list there are enough Microseconds that it works fine.

Here is my think working workaround... You might document this as if you give an initDate <= firstDate it sets it to initDate - 10 seconds


Future<DateTime> _showDatePicker(
      BuildContext context, DateTime currentValue) {
    DateTime _initDate = currentValue ?? widget.initialDate ?? DateTime.now(); 
    DateTime _firstDate = widget.firstDate ?? DateTime(1900);
    
    if ( _initDate.isBefore(_firstDate) )
        _firstDate = _initDate.subtract(Duration(seconds: 10)); 
    
    if (widget.datePicker != null) {
      return widget.datePicker(context);
    } else {
    
      return showDatePicker(
          context: context,
          selectableDayPredicate: widget.selectableDayPredicate,
          initialDatePickerMode:
              widget.initialDatePickerMode ?? DatePickerMode.day,
          // ignore: deprecated_member_use_from_same_package
          initialDate: _initDate,
          firstDate: _firstDate,
          lastDate: widget.lastDate ?? DateTime(2100));
    }
  }

@danvick danvick closed this as completed May 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants