我有两个日期选择器分别用于开始和结束日期。如果没有班级组,则结束日期选择器的初始日期使用开始日期,并且结束日期的检查工作正常,但是如果有班级组和日期作为结束日期,则初始日期使用结束日期日期,但bool检查无效。我要做的就是禁用某些日期,以使结束日期不应该少于开始日期。
下面是两个单独的日期选择器的代码。
_pickStartTime(BuildContext context) {
FocusScope.of(context).requestFocus(new FocusNode());
showDatePicker(
context: context,
firstDate: DateTime(DateTime.now().year - 5),
lastDate: DateTime(DateTime.now().year + 5),
initialDate: startPeriod,
builder: (BuildContext context, Widget child) {
return Theme(
data: ThemeData.light().copyWith(
primaryColor: AzuraColors.teal,
accentColor: AzuraColors.teal,
colorScheme: ColorScheme.light(primary: AzuraColors.teal),
buttonTheme: ButtonThemeData(textTheme: ButtonTextTheme.primary),
),
child: child,
);
},
).then((date) {
if (date != null && date != startPeriod) {
setState(() {
startPeriod = date;
_startPeriodController.value = TextEditingValue(
text: DateFormat.yMMMM().format(date).toString());
});
}
});
}
_pickEndTime(BuildContext context) {
FocusScope.of(context).requestFocus(new FocusNode());
ClassGroup classGroup = widget.modelData.mClassGroup;
bool _decideWhichDayToEnable(DateTime day) {
if (startPeriod != null && day != null && startPeriod.isAfter(day)) {
return false;
}
return true;
}
showDatePicker(
context: context,
firstDate: DateTime(DateTime.now().year - 5),
lastDate: DateTime(DateTime.now().year + 5),
initialDate: classGroup != null ? endPeriod : startPeriod,
selectableDayPredicate: _decideWhichDayToEnable,
builder: (BuildContext context, Widget child) {
return Theme(
data: ThemeData.light().copyWith(
primaryColor: AzuraColors.teal,
accentColor: AzuraColors.teal,
colorScheme: ColorScheme.light(primary: AzuraColors.teal),
buttonTheme: ButtonThemeData(textTheme: ButtonTextTheme.primary),
),
child: child,
);
},
).then((date) {
if (date != null && date != endPeriod) {
setState(() {
endPeriod = date;
_endPeriodController.value = TextEditingValue(
text: DateFormat.yMMMM().format(date).toString());
});
}
});
}