@@ -24,18 +24,10 @@ class HeaderCustomization extends StatefulWidget {
24
24
25
25
class HeaderCustomizationState extends State <HeaderCustomization > {
26
26
final List <String > _days = < String > ['S' , 'M' , 'T' , 'W' , 'T' , 'F' , 'S' ];
27
- DateRangePickerController _controller;
28
- String _headerString;
29
- PickerDateRange _visibleDateRange;
30
- int _selectedWeekDay;
31
-
32
- @override
33
- void initState () {
34
- _controller = DateRangePickerController ();
35
- _headerString = '' ;
36
- _selectedWeekDay = - 1 ;
37
- super .initState ();
38
- }
27
+ final DateRangePickerController _controller = DateRangePickerController ();
28
+ String _headerString = '' ;
29
+ late PickerDateRange _visibleDateRange;
30
+ int _selectedWeekDay = - 1 ;
39
31
40
32
@override
41
33
Widget build (BuildContext context) {
@@ -45,90 +37,89 @@ class HeaderCustomizationState extends State<HeaderCustomization> {
45
37
46
38
return Scaffold (
47
39
body: Column (
48
- mainAxisAlignment: MainAxisAlignment .center,
40
+ mainAxisAlignment: MainAxisAlignment .center,
41
+ children: < Widget > [
42
+ Container (
43
+ child: Row (
49
44
children: < Widget > [
50
45
Container (
51
- child: Row (
52
- children: < Widget > [
53
- Container (
54
- margin: const EdgeInsets .symmetric (horizontal: 4 ),
55
- width: viewHeaderCellWidth,
56
- child: IconButton (
57
- icon: Icon (Icons .arrow_left),
58
- color: Colors .black,
59
- onPressed: () {
60
- setState (() {
61
- _controller.backward ();
62
- });
63
- },
64
- )),
65
- Expanded (
66
- child: Text (_headerString,
67
- textAlign: TextAlign .center,
68
- style: TextStyle (fontSize: 25 , color: Colors .black)),
69
- ),
70
- Container (
71
- margin: const EdgeInsets .symmetric (horizontal: 4 ),
72
- width: viewHeaderCellWidth,
73
- child: IconButton (
74
- icon: Icon (Icons .arrow_right),
75
- color: Colors .black,
76
- onPressed: () {
77
- setState (() {
78
- _controller.forward ();
79
- });
80
- },
81
- )),
82
- ],
46
+ margin: const EdgeInsets .symmetric (horizontal: 4 ),
47
+ width: viewHeaderCellWidth,
48
+ child: IconButton (
49
+ icon: Icon (Icons .arrow_left),
50
+ color: Colors .black,
51
+ onPressed: () {
52
+ setState (() {
53
+ _controller.backward !();
54
+ });
55
+ },
83
56
)),
57
+ Expanded (
58
+ child: Text (_headerString,
59
+ textAlign: TextAlign .center,
60
+ style: TextStyle (fontSize: 25 , color: Colors .black)),
61
+ ),
84
62
Container (
85
- height: viewHeaderHeight,
86
- width: width,
87
63
margin: const EdgeInsets .symmetric (horizontal: 4 ),
88
- child: ListView .builder (
89
- itemCount: _days.length,
90
- scrollDirection: Axis .horizontal,
91
- itemBuilder: (BuildContext context, int index) {
92
- return GestureDetector (
93
- onTap: ()
94
- {
95
- final int selectedIndex = index == 0 ? 7 : index;
96
- _selectedWeekDay = selectedIndex;
97
- _updateSelectedDates ();
98
- },
99
- child: Container (
100
- alignment: Alignment .center,
101
- width: viewHeaderCellWidth,
102
- height: viewHeaderHeight,
103
- child: Text (_days[index]),
104
- ));
105
- })),
106
- Container (
107
- margin: const EdgeInsets .symmetric (horizontal: 4 ),
108
- child: SfDateRangePicker (
109
- selectionMode: DateRangePickerSelectionMode .multiple,
110
- controller: _controller,
111
- headerHeight: 0 ,
112
- onViewChanged: viewChanged,
113
- monthViewSettings:
114
- DateRangePickerMonthViewSettings (viewHeaderHeight: 0 ),
115
- ),
116
- )
64
+ width: viewHeaderCellWidth,
65
+ child: IconButton (
66
+ icon: Icon (Icons .arrow_right),
67
+ color: Colors .black,
68
+ onPressed: () {
69
+ setState (() {
70
+ _controller.forward !();
71
+ });
72
+ },
73
+ )),
117
74
],
118
- ));
75
+ )),
76
+ Container (
77
+ height: viewHeaderHeight,
78
+ width: width,
79
+ margin: const EdgeInsets .symmetric (horizontal: 4 ),
80
+ child: ListView .builder (
81
+ itemCount: _days.length,
82
+ scrollDirection: Axis .horizontal,
83
+ itemBuilder: (BuildContext context, int index) {
84
+ return GestureDetector (
85
+ onTap: () {
86
+ final int selectedIndex = index == 0 ? 7 : index;
87
+ _selectedWeekDay = selectedIndex;
88
+ _updateSelectedDates ();
89
+ },
90
+ child: Container (
91
+ alignment: Alignment .center,
92
+ width: viewHeaderCellWidth,
93
+ height: viewHeaderHeight,
94
+ child: Text (_days[index]),
95
+ ));
96
+ })),
97
+ Container (
98
+ margin: const EdgeInsets .symmetric (horizontal: 4 ),
99
+ child: SfDateRangePicker (
100
+ selectionMode: DateRangePickerSelectionMode .multiple,
101
+ controller: _controller,
102
+ headerHeight: 0 ,
103
+ onViewChanged: viewChanged,
104
+ monthViewSettings:
105
+ DateRangePickerMonthViewSettings (viewHeaderHeight: 0 ),
106
+ ),
107
+ )
108
+ ],
109
+ ));
119
110
}
120
111
121
112
void _updateSelectedDates () {
122
- if (_selectedWeekDay == - 1 || _visibleDateRange == null ) {
113
+ if (_selectedWeekDay == - 1 ) {
123
114
return ;
124
115
}
125
116
126
117
final List <DateTime > selectedDates = < DateTime > [];
127
- DateTime date = _visibleDateRange.startDate;
128
- while (date.isBefore (_visibleDateRange.endDate) ||
129
- (date.year == _visibleDateRange.endDate.year &&
130
- date.month == _visibleDateRange.endDate.month &&
131
- date.day == _visibleDateRange.endDate.day)) {
118
+ DateTime date = _visibleDateRange.startDate! ;
119
+ while (date.isBefore (_visibleDateRange.endDate! ) ||
120
+ (date.year == _visibleDateRange.endDate! .year &&
121
+ date.month == _visibleDateRange.endDate! .month &&
122
+ date.day == _visibleDateRange.endDate! .day)) {
132
123
if (_selectedWeekDay == date.weekday) {
133
124
selectedDates.add (date);
134
125
}
@@ -141,14 +132,13 @@ class HeaderCustomizationState extends State<HeaderCustomization> {
141
132
142
133
void viewChanged (DateRangePickerViewChangedArgs args) {
143
134
_visibleDateRange = args.visibleDateRange;
144
- final int daysCount = (args.visibleDateRange.endDate
145
- .difference (args.visibleDateRange.startDate)
135
+ final int daysCount = (args.visibleDateRange.endDate!
136
+ .difference (args.visibleDateRange.startDate! )
146
137
.inDays);
147
- var middleDate = (daysCount ~ / 2 ).toInt ();
148
138
final DateTime date =
149
- args.visibleDateRange.startDate.add (Duration (days: middleDate ));
139
+ args.visibleDateRange.startDate! .add (Duration (days: daysCount ~ / 2 ));
150
140
_headerString = DateFormat ('MMMM yyyy' ).format (date).toString ();
151
- SchedulerBinding .instance.addPostFrameCallback ((duration) {
141
+ SchedulerBinding .instance! .addPostFrameCallback ((duration) {
152
142
setState (() {});
153
143
});
154
144
}
0 commit comments