We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to restrict date range picker within the date limit in the Flutter Date Range Picker (SfDateRangePicker)?

Platform: Flutter |
Control: SfDateRangePicker

In the Flutter Date Range Picker, you can restrict the swiping behavior using the `minDate` and `maxDate` property.

Step 1:

In initState(), set the default values for min and max dates.

late DateTime _minDate, _maxDate;
 
@override
void initState() {
  _minDate=DateTime(2020,3,5,9,0,0);
  _maxDate=DateTime(2020,3,25,9,0,0);
  super.initState();
}

 

Step 2:

Place the date picker inside the body of the Scaffold widget with the mentioned min and max date.

body: Card(
  margin: const EdgeInsets.fromLTRB(50, 150, 50, 150),
  child: SfDateRangePicker(
    view: DateRangePickerView.month,
    minDate: _minDate,
    maxDate: _maxDate,
  ),
)

 

Step 3: 

Please find the entire code for the date restriction as follows.

import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_datepicker/datepicker.dart';
 
void main() => runApp(SwipeRestriction());
 
class SwipeRestriction extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false ,
      home: ViewRestriction(),
    );
  }
}
 
class ViewRestriction extends StatefulWidget {
  @override
  _ViewRestrictionState createState() => _ViewRestrictionState();
}
 
class _ViewRestrictionState extends State<ViewRestriction> {
  late DateTime _minDate, _maxDate;
 
  @override
  void initState() {
    _minDate=DateTime(2020,3,5,9,0,0);
    _maxDate=DateTime(2020,3,25,9,0,0);
    super.initState();
  }
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Card(
          margin: const EdgeInsets.fromLTRB(40, 150, 40, 150),
          child: SfDateRangePicker(
            view: DateRangePickerView.month,
            minDate: _minDate,
            maxDate: _maxDate,
          ),
        )
    );
  }
}

 

View sample in Github.

Screenshot:

 

date restriction

 

 

ADD COMMENT
You must log in to leave a comment
Comments
Jawad
Jan 06, 2022

How to restrict a user to only select week days between monday and sunday. Like no repetition of any week day.

Reply
Indumathi Ravichandran [Syncfusion]
Jan 06, 2022

Hi Jawad,

Thank you for contacting Syncfusion support.

Regarding Query: How to restrict a user to only select week days between monday and sunday.

Based on the shared information, we have checked and your requirement is “Restrict the date selection except Monday and Sunday dates”. By using the blackoutDates property, you can restrict the selection for specific dates through onViewChanged callback of the calendar. We have prepared the simple sample for the same. Please find the sample from the following link.

Sample link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/active-dates-flutter-date-range-picker-main329720549.zip

Code snippet:

void viewChanged(DateRangePickerViewChangedArgs args) {
  DateTime date;
  DateTime startDate = args.visibleDateRange.startDate!;
  DateTime endDate = args.visibleDateRange.endDate!;
  List<DateTime> _blackDates = <DateTime>[];
  for (date = startDate;
      date.isBefore(endDate) || date == endDate;
      date = date.add(const Duration(days: 1))) {
    if (date.weekday == DateTime.monday ||
        date.weekday == DateTime.sunday ) {
      continue;
    }

    _blackDates.add(date);
  }
  SchedulerBinding.instance!.addPostFrameCallback((timeStamp) {
    setState(() {
      _blackoutDateCollection = _blackDates;
    });
  });
}

We have a KB document to restrict the date selection. Please find the KB document from the following link.

KB link: https://www.syncfusion.com/kb/12075/how-to-add-active-dates-in-the-flutter-date-range-picker-sfdaterangepicker

We hope that this helps you. Please let us know if you need further assistance.

Regards, Indumathi R

Reply

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile