How to disable the input in the Flutter SfDateRangePicker?
In the Flutter date range picker, the user interaction can be disabled by using the selectableDayPredicate property of the date range picker.
STEP 1: In initState(), initialize the default values.
late List<DateTime> _activeDates; final DateRangePickerController _controller = DateRangePickerController(); @override void initState() { DateTime today = DateTime.now(); today = DateTime(today.year, today.month, today.day); _activeDates = [ today, today.add(const Duration(days: 1)), today.add(const Duration(days: 2)), today.add(const Duration(days: 3)), today.add(const Duration(days: 4)), today.add(const Duration(days: 5)), ]; super.initState(); }
STEP 2: Set the selectionMode as range and set the initialSelectedRange. To make the disabled dates look as normal, the disabledDatesTextStyle is used.
child: SfDateRangePicker( controller: _controller, view: DateRangePickerView.month, selectionMode: DateRangePickerSelectionMode.range, initialSelectedRange: PickerDateRange(_activeDates[0], _activeDates[5]), startRangeSelectionColor: Colors.purple, endRangeSelectionColor: Colors.purple, rangeSelectionColor: Colors.purpleAccent, onSelectionChanged: selectionChanged, selectableDayPredicate: predicateCallback, monthCellStyle: const DateRangePickerMonthCellStyle( disabledDatesTextStyle: TextStyle( color: Colors.black87, fontSize: 13, fontFamily: 'Roboto')), ),
STEP 3: Using the selectableDayPredicate callback, set the specified dates for selection.
bool predicateCallback(DateTime date) { for (int i = 0; i < _activeDates.length; i++) { if (_activeDates[i] == date) { return true; } } return false; }
STEP 4: Using selectionChanged callback, set the selected range of the picker as specified active dates.
void selectionChanged(DateRangePickerSelectionChangedArgs args) { _controller.selectedRange = PickerDateRange(_activeDates[0], _activeDates[5]); }
Conclusion
I hope you enjoyed learning about how to disable the input in the Flutter DateRangePicker.
You can refer to our Flutter DateRangePicker feature tour page to know about its other groundbreaking feature representations documentation and how to quickly get started for configuration specifications. You can also explore our Flutter Date Range Picker example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!