|
1 | | -# mon-max-date-range-picker-flutter |
2 | | -How to restrict the swiping behavior in Flutter date range picker (SfDateRangePicker)? |
| 1 | +# How to restrict date range picker within the date limit in Flutter date range picker (SfDateRangePicker)? |
| 2 | + |
| 3 | +In the flutter date range picker, you can restrict the swiping behavior using the `minDate` and `maxDate` property. |
| 4 | + |
| 5 | +## Step 1: |
| 6 | +In initState(), set the default values for min and max dates. |
| 7 | + |
| 8 | +```xml |
| 9 | +DateTime _minDate, _maxDate; |
| 10 | + |
| 11 | +@override |
| 12 | +void initState() { |
| 13 | + _minDate=DateTime(2020,3, 5,9,0,0); |
| 14 | + _maxDate=DateTime(2020,3,25,9,0,0); |
| 15 | + super.initState(); |
| 16 | +} |
| 17 | +``` |
| 18 | +## Step 2: |
| 19 | +Place the date picker inside the body of the Scaffold widget with the mentioned min and max date. |
| 20 | + |
| 21 | +```xml |
| 22 | +body: Card( |
| 23 | + margin: const EdgeInsets.fromLTRB(50, 150, 50, 150), |
| 24 | + child: SfDateRangePicker( |
| 25 | + view: DateRangePickerView.month, |
| 26 | + minDate: _minDate, |
| 27 | + maxDate: _maxDate, |
| 28 | + ), |
| 29 | +) |
| 30 | +``` |
| 31 | + |
| 32 | + |
| 33 | +## Step 3: |
| 34 | +Please find the entire code for the date restriction as follows. |
| 35 | + |
| 36 | +```xml |
| 37 | +import 'package:flutter/material.dart'; |
| 38 | +import 'package:syncfusion_flutter_datepicker/datepicker.dart'; |
| 39 | + |
| 40 | +void main() => runApp(SwipeRestriction()); |
| 41 | + |
| 42 | +class SwipeRestriction extends StatelessWidget { |
| 43 | + // This widget is the root of your application. |
| 44 | + @override |
| 45 | + Widget build(BuildContext context) { |
| 46 | + return MaterialApp( |
| 47 | + debugShowCheckedModeBanner: false , |
| 48 | + home: ViewRestriction(), |
| 49 | + ); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +class ViewRestriction extends StatefulWidget { |
| 54 | + @override |
| 55 | + _ViewRestrictionState createState() => _ViewRestrictionState(); |
| 56 | +} |
| 57 | + |
| 58 | +class _ViewRestrictionState extends State<ViewRestriction> { |
| 59 | + DateTime _minDate, _maxDate; |
| 60 | + |
| 61 | + @override |
| 62 | + void initState() { |
| 63 | + _minDate=DateTime(2020,3, 5,9,0,0); |
| 64 | + _maxDate=DateTime(2020,3,25,9,0,0); |
| 65 | + super.initState(); |
| 66 | + } |
| 67 | + |
| 68 | + @override |
| 69 | + Widget build(BuildContext context) { |
| 70 | + return Scaffold( |
| 71 | + body: Card( |
| 72 | + margin: const EdgeInsets.fromLTRB(40, 150, 40, 150), |
| 73 | + child: SfDateRangePicker( |
| 74 | + view: DateRangePickerView.month, |
| 75 | + minDate: _minDate, |
| 76 | + maxDate: _maxDate, |
| 77 | + ), |
| 78 | + ) |
| 79 | + ); |
| 80 | + } |
| 81 | +} |
| 82 | +``` |
0 commit comments