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 |
Published Date: April 2, 2020 |
Last Revised Date: April 22, 2021

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

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