build method
Build the picker.
context
is passed BuildContext. currentDateTime
is passed the currently selected DateTime.
ピッカーのビルドを行ないます。
context
にBuildContextが渡されます。currentDateTime
に現在選択されているDateTimeが渡されます。
Implementation
Future<DateTime?> build(
BuildContext context,
DateTime? currentDateTime,
) async {
assert(
begin == null ||
end == null ||
(begin != null && end != null && begin!.isBefore(end!)),
"[begin] must be before [end].",
);
final theme = Theme.of(context);
DateTime? res;
final now = DateTime.now();
final startYear = begin?.year ?? (now.year - 10);
final endYear = end?.year ?? (now.year + 10);
final selectedYear = ((currentDateTime?.year ??
defaultDateTime?.year ??
begin?.year ??
end?.year ??
now.year) -
startYear)
.limit(0, endYear - startYear);
var selectedMonth = ((currentDateTime?.month ??
defaultDateTime?.month ??
begin?.month ??
end?.month ??
now.month) -
1)
.limit(0, 12);
if (selectedYear == 0) {
selectedMonth = selectedMonth - ((begin?.month ?? 1) - 1);
}
await _Picker(
height: 240,
backgroundColor: backgroundColor ?? theme.colorScheme.surface,
containerColor: backgroundColor ?? theme.colorScheme.surface,
headerColor: backgroundColor ?? theme.colorScheme.surface,
textStyle: TextStyle(color: color ?? theme.colorScheme.onSurface),
confirmText: confirmText,
cancelText: cancelText,
selecteds: [
selectedYear,
selectedMonth,
],
adapter: _PickerDataAdapter<int>(
data: [
for (var y = startYear; y < endYear; y++) ...[
() {
final year = DateTime(y, 1, 1);
final startMonth = y == startYear ? begin?.month ?? 1 : 1;
final endMonth = y == endYear - 1 ? end?.month ?? 12 : 12;
return _PickerItem(
text: Text(
"${year.format(yearFormat)}$yearSuffix",
style: TextStyle(
color: color ?? theme.colorScheme.onSurface,
),
),
value: y,
children: [
for (var m = startMonth - 1; m < endMonth; m++)
_PickerItem(
text: Text(
"${(m + 1).format("00")}$monthSuffix",
style: TextStyle(
color: color ?? theme.colorScheme.onSurface,
),
),
value: m,
),
],
);
}(),
],
],
),
changeToFirst: true,
hideHeader: false,
onConfirm: (_Picker picker, List<int> value) {
var month = value[1] + 1;
if (value[0] == 0) {
month = month + ((begin?.month ?? 1) - 1);
}
if (lastDayOfMonth == true) {
res = DateTime(value[0] + startYear, month + 1, 0);
} else {
res = DateTime(value[0] + startYear, month, day ?? 1);
}
},
).showModal(context);
return res;
}