Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@
In the flutter date range picker, you can select the entire week when selecting a single day using the `onSelectionChanged` callback and `enableSwipeSelection` property.

## Step 1:
In initState(), initialize the controller for date range picker.
Inside the state, initialize the controller for date range picker.

```xml
DateRangePickerController _controller;
@override
void initState() {
// TODO: implement initState
_controller = DateRangePickerController();
super.initState();
}
final DateRangePickerController _controller = DateRangePickerController();
```
## Step 2:
Place the date range picker inside the body of the Scaffold widget, set the `selectionMode` as range selection and trigger the `onSelectionChanged` callback of the flutter date picker.
Expand Down Expand Up @@ -45,8 +39,8 @@ void selectionChanged(DateRangePickerSelectionChangedArgs args) {
int endDayOfWeek = (firstDayOfWeek - 1) % 7;
endDayOfWeek = endDayOfWeek < 0? 7 + endDayOfWeek : endDayOfWeek;
PickerDateRange ranges = args.value;
DateTime date1 = ranges.startDate;
DateTime date2 = ranges.endDate?? ranges.startDate;
DateTime date1 = ranges.startDate!;
DateTime date2 = (ranges.endDate?? ranges.startDate)!;
if(date1.isAfter(date2))
{
var date=date1;
Expand Down
40 changes: 16 additions & 24 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class WeekSelection extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false ,
debugShowCheckedModeBanner: false,
home: WeekSelectionInPicker(),
);
}
Expand All @@ -20,14 +20,7 @@ class WeekSelectionInPicker extends StatefulWidget {
}

class _WeekSelectionInPickerState extends State<WeekSelectionInPicker> {
DateRangePickerController _controller;

@override
void initState() {
// TODO: implement initState
_controller = DateRangePickerController();
super.initState();
}
final DateRangePickerController _controller = DateRangePickerController();

@override
Widget build(BuildContext context) {
Expand All @@ -41,7 +34,8 @@ class _WeekSelectionInPickerState extends State<WeekSelectionInPicker> {
view: DateRangePickerView.month,
selectionMode: DateRangePickerSelectionMode.range,
onSelectionChanged: selectionChanged,
monthViewSettings: DateRangePickerMonthViewSettings(enableSwipeSelection: false),
monthViewSettings:
DateRangePickerMonthViewSettings(enableSwipeSelection: false),
),
)
],
Expand All @@ -51,33 +45,31 @@ class _WeekSelectionInPickerState extends State<WeekSelectionInPicker> {
);
}


void selectionChanged(DateRangePickerSelectionChangedArgs args) {
int firstDayOfWeek = DateTime.sunday % 7;
int endDayOfWeek = (firstDayOfWeek - 1) % 7;
endDayOfWeek = endDayOfWeek < 0? 7 + endDayOfWeek : endDayOfWeek;
endDayOfWeek = endDayOfWeek < 0 ? 7 + endDayOfWeek : endDayOfWeek;
PickerDateRange ranges = args.value;
DateTime date1 = ranges.startDate;
DateTime date2 = ranges.endDate?? ranges.startDate;
if(date1.isAfter(date2))
{
var date=date1;
date1=date2;
date2=date;
}
DateTime date1 = ranges.startDate!;
DateTime date2 = (ranges.endDate ?? ranges.startDate)!;
if (date1.isAfter(date2)) {
var date = date1;
date1 = date2;
date2 = date;
}
int day1 = date1.weekday % 7;
int day2 = date2.weekday % 7;

DateTime dat1 = date1.add(Duration(days: (firstDayOfWeek - day1)));
DateTime dat2 = date2.add(Duration(days: (endDayOfWeek - day2)));

if( !isSameDate(dat1, ranges.startDate)|| !isSameDate(dat2,ranges.endDate))
{
if (!isSameDate(dat1, ranges.startDate) ||
!isSameDate(dat2, ranges.endDate)) {
_controller.selectedRange = PickerDateRange(dat1, dat2);
}
}

bool isSameDate(DateTime date1, DateTime date2) {
bool isSameDate(DateTime? date1, DateTime? date2) {
if (date2 == date1) {
return true;
}
Expand All @@ -88,4 +80,4 @@ class _WeekSelectionInPickerState extends State<WeekSelectionInPicker> {
date1.year == date2.year &&
date1.day == date2.day;
}
}
}
Loading