flutter_date_pickers icon indicating copy to clipboard operation
flutter_date_pickers copied to clipboard

Clear selection RangePicker

Open ReniDelonzek opened this issue 5 years ago • 4 comments

When I disable some days and the user clicks on a day where both the previous and the next are disabled, he gets stuck, as he cannot select any range or deselect the day, a possible solution for this would be a way to clear the selection (selectedPeriod can be null for example)

Screenshot_1597974583

ReniDelonzek avatar Aug 21 '20 01:08 ReniDelonzek

@ReniDelonzek while I am thinking about best desition for such kind of situation you can use RangePicker.onSelectionError callback. It provides information about error that occurred during user's selection. So if callback is called you can manually set RangePicker.selectedPeriod to null there and call setState after change.

MariaMelnik avatar Aug 24 '20 22:08 MariaMelnik

If u set RangePicker.selectedPeriod = null, the app crashes, other solution for this? i have the same problem :S

jccd1996 avatar Oct 17 '20 00:10 jccd1996

@jccd1996 RangePicker has onSelectionError property. It provides information about period user would like to select but can't because of disabled dates. You can use this information to change selection something like this:

  void _onSelectionError(UnselectablePeriodException exception) {
    DatePeriod errorPeriod = exception.period;

    // If user supposed to set another start of the period.
    bool selectStart = _selectedPeriod.start != errorPeriod.start;

    DateTime newSelection = selectStart
        ? errorPeriod.start
        : errorPeriod.end;
    
    DatePeriod newPeriod = DatePeriod(newSelection, newSelection);

    setState(() {
      _selectedPeriod = newPeriod;
    });
  }

Pass this function to RangePicker constructor (look at onSelectionError).

RangePicker(
            selectedPeriod: _selectedPeriod,
            onChanged: _onSelectedDateChanged,
            firstDate: _firstDate,
            lastDate: _lastDate,
            datePickerStyles: styles,
            eventDecorationBuilder: _eventDecorationBuilder,
            selectableDayPredicate: _isSelectableCustom,
            onSelectionError: _onSelectionError
)

MariaMelnik avatar Oct 17 '20 08:10 MariaMelnik

@MariaMelnik Thank you so much!, this solution its exactly that was i looking for!

jccd1996 avatar Oct 20 '20 02:10 jccd1996