forked from egovernments/punjab-mgramseva
/
nested_date_picker.dart
254 lines (243 loc) · 10.7 KB
/
nested_date_picker.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import 'package:flutter/material.dart';
import 'package:mgramseva/utils/Locilization/application_localizations.dart';
import 'package:mgramseva/utils/common_methods.dart';
import 'package:mgramseva/utils/date_formats.dart';
import 'package:mgramseva/utils/models.dart';
import 'package:mgramseva/utils/Constants/I18KeyConstants.dart';
import 'package:mgramseva/widgets/custom_overlay/show_overlay.dart';
class NestedDatePicker extends StatefulWidget {
final ValueChanged<DatePeriod?> onSelectionOfDate;
final List<dynamic>? yearsWithMonths;
final DatePeriod? selectedMonth;
final int maximumYears;
final double? left;
final double? top;
final DateType dateType;
const NestedDatePicker(
{Key? key,
required this.onSelectionOfDate,
this.left,
this.top,
this.yearsWithMonths,
this.selectedMonth,
this.maximumYears = 5,
this.dateType = DateType.YEAR})
: super(key: key);
@override
State<NestedDatePicker> createState() => _NestedDatePickerState();
}
class _NestedDatePickerState extends State<NestedDatePicker> {
late List<dynamic> yearsWithMonths;
DatePeriod? selectedMonth;
@override
void initState() {
if (widget.dateType == DateType.YEAR) {
yearsWithMonths = widget.yearsWithMonths ??
CommonMethods.getFinancialYearList(widget.maximumYears);
} else {
yearsWithMonths = CommonMethods.getMonthsOfFinancialYear();
}
if (widget.dateType == DateType.YEAR) {
if (widget.selectedMonth != null) {
DatePeriod? date;
for (YearWithMonths yearWithMonth in yearsWithMonths) {
if ((widget.selectedMonth?.startDate.toString() == yearWithMonth.year.startDate.toString()) &&
(widget.selectedMonth?.endDate.toString() == yearWithMonth.year.endDate.toString())) {
date = yearWithMonth.year;
}
if(date == null)
for (var month in yearWithMonth.monthList) {
if (widget.selectedMonth?.endDate.toString() == month.endDate.toString()) {
date = month;
}
}
}
selectedMonth = date ?? yearsWithMonths.first.year;
} else {
selectedMonth ??= yearsWithMonths.first.year;
}
} else {
DatePeriod? date;
for (var month in yearsWithMonths) {
if (widget.selectedMonth?.endDate.toString() ==
month.endDate.toString()) {
date = month;
}
}
selectedMonth = date ?? yearsWithMonths.first;
}
super.initState();
}
@override
Widget build(BuildContext context) {
var height = MediaQuery.of(context).size.height;
return Positioned(
left: widget.left,
top: widget.top,
child: Material(
color: Colors.transparent,
child: Container(
constraints:
BoxConstraints(maxHeight: (height - (widget.top ?? 0))),
padding: EdgeInsets.symmetric(vertical: 5, horizontal: 5),
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
bottomRight: Radius.circular(5.0),
bottomLeft: Radius.circular(5.0),
),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(.5),
blurRadius: 20.0, // soften the shadow
spreadRadius: 0.0, //extend the shadow
offset: Offset(
5.0, // Move to right 10 horizontally
5.0, // Move to bottom 10 Vertically
),
)
],
),
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: widget.dateType == DateType.YEAR
? List.generate(yearsWithMonths.length, (index) {
var yearWithMonths = yearsWithMonths[index];
return Wrap(
direction: Axis.vertical,
children: [
Tooltip(
padding: EdgeInsets.all(5),
message:
'${ApplicationLocalizations.of(context).translate(i18.dashboard.YEAR_TOOL_TIP_MESSAGE)}',
child: Container(
width: 200,
decoration: index ==
yearWithMonths.monthList.length -
1
? BoxDecoration(
color: index % 2 == 0
? Color.fromRGBO(
238, 238, 238, 1)
: Color.fromRGBO(
255, 255, 255, 1),
borderRadius: BorderRadius.only(
bottomRight:
Radius.circular(5.0),
bottomLeft:
Radius.circular(5.0),
))
: BoxDecoration(
color: index % 2 == 0
? Color.fromRGBO(
238, 238, 238, 1)
: Color.fromRGBO(
255, 255, 255, 1),
),
padding: EdgeInsets.symmetric(
vertical: 8, horizontal: 8),
child: Wrap(
spacing: 5,
crossAxisAlignment:
WrapCrossAlignment.center,
alignment: WrapAlignment.spaceBetween,
children: [
InkWell(
onTap: () => onSelectionOfExpansion(
yearWithMonths),
child: Text(
'${DateFormats.getMonthAndYear(yearWithMonths.year, context)}',
style: TextStyle(
fontSize: 16,
fontWeight:
yearStatus(yearWithMonths)
? FontWeight.bold
: FontWeight.normal),
),
),
Radio(
value: yearWithMonths.year,
groupValue: selectedMonth,
onChanged: onSelectionOfDate)
],
),
),
),
Visibility(
visible: monthStatus(yearWithMonths),
child: Wrap(
direction: Axis.vertical,
children: List.generate(
yearWithMonths.monthList.length,
(monthIndex) {
var date = yearWithMonths
.monthList[monthIndex];
return _dateView(date,
(monthIndex + index) % 2 == 0);
}).toList()),
)
],
);
})
: List.generate(yearsWithMonths.length, (index) {
var date = yearsWithMonths[index];
return _dateView(date, index % 2 == 0);
})),
))));
}
Widget _dateView(DatePeriod date, bool flag) {
return InkWell(
onTap: () => onSelectionOfDate(date),
child: Container(
width: 200,
decoration: BoxDecoration(
color: flag
? Color.fromRGBO(255, 255, 255, 1)
: Color.fromRGBO(238, 238, 238, 1)),
padding: EdgeInsets.symmetric(vertical: 8, horizontal: 8),
child: Container(
width: 195,
alignment: Alignment.centerRight,
child: Wrap(
spacing: 5,
crossAxisAlignment: WrapCrossAlignment.center,
alignment: WrapAlignment.spaceBetween,
children: [
Text(
'${DateFormats.getMonthAndYear(date, context)}',
style: TextStyle(
fontSize: 16,
fontWeight: selectedMonth == date
? FontWeight.bold
: FontWeight.normal),
),
Radio(
value: date,
groupValue: selectedMonth,
onChanged: onSelectionOfDate)
],
),
),
),
);
}
void onSelectionOfDate(datePeriod) {
if (datePeriod?.dateType != DateType.MONTH)
yearsWithMonths.forEach((e) => e.isExpanded = false);
CustomOVerlay.removeOverLay();
widget.onSelectionOfDate(datePeriod);
}
void onSelectionOfExpansion(YearWithMonths yearWithMonths) {
yearsWithMonths.forEach((e) {
if (e != yearWithMonths) e.isExpanded = false;
});
setState(() {
yearWithMonths.isExpanded = !yearWithMonths.isExpanded;
});
}
bool yearStatus(YearWithMonths yearWithMonths) =>
selectedMonth == yearWithMonths.year;
bool monthStatus(YearWithMonths yearWithMonths) =>
yearWithMonths.monthList.contains(selectedMonth) ||
yearWithMonths.isExpanded;
}