Unverified Commit 2f74e305 authored by Darren Austin's avatar Darren Austin Committed by GitHub

Fixed a problem with invalid dates when switching back to calendar mode in the...

Fixed a problem with invalid dates when switching back to calendar mode in the date range picker. (#67159)
parent 19b2aea6
...@@ -303,6 +303,17 @@ class _DateRangePickerDialogState extends State<_DateRangePickerDialog> { ...@@ -303,6 +303,17 @@ class _DateRangePickerDialogState extends State<_DateRangePickerDialog> {
break; break;
case DatePickerEntryMode.input: case DatePickerEntryMode.input:
// Validate the range dates
if (_selectedStart != null &&
(_selectedStart!.isBefore(widget.firstDate) || _selectedStart!.isAfter(widget.lastDate))) {
_selectedStart = null;
// With no valid start date, having an end date makes no sense for the UI.
_selectedEnd = null;
}
if (_selectedEnd != null &&
(_selectedEnd!.isBefore(widget.firstDate) || _selectedEnd!.isAfter(widget.lastDate))) {
_selectedEnd = null;
}
// If invalid range (start after end), then just use the start date // If invalid range (start after end), then just use the start date
if (_selectedStart != null && _selectedEnd != null && _selectedStart!.isAfter(_selectedEnd!)) { if (_selectedStart != null && _selectedEnd != null && _selectedStart!.isAfter(_selectedEnd!)) {
_selectedEnd = null; _selectedEnd = null;
...@@ -313,7 +324,7 @@ class _DateRangePickerDialogState extends State<_DateRangePickerDialog> { ...@@ -313,7 +324,7 @@ class _DateRangePickerDialogState extends State<_DateRangePickerDialog> {
}); });
} }
void _handleStartDateChanged(DateTime date) { void _handleStartDateChanged(DateTime? date) {
setState(() => _selectedStart = date); setState(() => _selectedStart = date);
} }
......
...@@ -64,10 +64,10 @@ class InputDateRangePicker extends StatefulWidget { ...@@ -64,10 +64,10 @@ class InputDateRangePicker extends StatefulWidget {
final DateTime lastDate; final DateTime lastDate;
/// Called when the user changes the start date of the selected range. /// Called when the user changes the start date of the selected range.
final ValueChanged<DateTime>? onStartDateChanged; final ValueChanged<DateTime?>? onStartDateChanged;
/// Called when the user changes the end date of the selected range. /// Called when the user changes the end date of the selected range.
final ValueChanged<DateTime>? onEndDateChanged; final ValueChanged<DateTime?>? onEndDateChanged;
/// The text that is displayed at the top of the header. /// The text that is displayed at the top of the header.
/// ///
...@@ -205,7 +205,7 @@ class InputDateRangePickerState extends State<InputDateRangePicker> { ...@@ -205,7 +205,7 @@ class InputDateRangePickerState extends State<InputDateRangePicker> {
setState(() { setState(() {
_startInputText = text; _startInputText = text;
_startDate = _parseDate(text); _startDate = _parseDate(text);
widget.onStartDateChanged?.call(_startDate!); widget.onStartDateChanged?.call(_startDate);
}); });
if (widget.autovalidate) { if (widget.autovalidate) {
validate(); validate();
...@@ -216,7 +216,7 @@ class InputDateRangePickerState extends State<InputDateRangePicker> { ...@@ -216,7 +216,7 @@ class InputDateRangePickerState extends State<InputDateRangePicker> {
setState(() { setState(() {
_endInputText = text; _endInputText = text;
_endDate = _parseDate(text); _endDate = _parseDate(text);
widget.onEndDateChanged?.call(_endDate!); widget.onEndDateChanged?.call(_endDate);
}); });
if (widget.autovalidate) { if (widget.autovalidate) {
validate(); validate();
......
...@@ -256,6 +256,52 @@ void main() { ...@@ -256,6 +256,52 @@ void main() {
}); });
}); });
group('Toggle from input entry mode validates dates', () {
setUp(() {
initialEntryMode = DatePickerEntryMode.input;
});
testWidgets('Invalid start date', (WidgetTester tester) async {
// Invalid start date should have neither a start nor end date selected in
// calendar mode
await preparePicker(tester, (Future<DateTimeRange> range) async {
await tester.enterText(find.byType(TextField).at(0), '12/27/1918');
await tester.enterText(find.byType(TextField).at(1), '12/25/2016');
await tester.tap(find.byIcon(Icons.calendar_today));
await tester.pumpAndSettle();
expect(find.text('Start Date'), findsOneWidget);
expect(find.text('End Date'), findsOneWidget);
});
});
testWidgets('Invalid end date', (WidgetTester tester) async {
// Invalid end date should only have a start date selected
await preparePicker(tester, (Future<DateTimeRange> range) async {
await tester.enterText(find.byType(TextField).at(0), '12/24/2016');
await tester.enterText(find.byType(TextField).at(1), '12/25/2050');
await tester.tap(find.byIcon(Icons.calendar_today));
await tester.pumpAndSettle();
expect(find.text('Dec 24'), findsOneWidget);
expect(find.text('End Date'), findsOneWidget);
});
});
testWidgets('Invalid range', (WidgetTester tester) async {
// Start date after end date should just use the start date
await preparePicker(tester, (Future<DateTimeRange> range) async {
await tester.enterText(find.byType(TextField).at(0), '12/25/2016');
await tester.enterText(find.byType(TextField).at(1), '12/24/2016');
await tester.tap(find.byIcon(Icons.calendar_today));
await tester.pumpAndSettle();
expect(find.text('Dec 25'), findsOneWidget);
expect(find.text('End Date'), findsOneWidget);
});
});
});
testWidgets('OK Cancel button layout', (WidgetTester tester) async { testWidgets('OK Cancel button layout', (WidgetTester tester) async {
Widget buildFrame(TextDirection textDirection) { Widget buildFrame(TextDirection textDirection) {
return MaterialApp( return MaterialApp(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment