Skip to content

Commit

Permalink
Date picker special labeling for currentDate with localization and te… (
Browse files Browse the repository at this point in the history
#116433)

* Date picker special labeling for currentDate with localization and tests.

* Updated CalendarDatePicker semantics test

* removed recursive import

* changed labeling for current date to be less verbose
  • Loading branch information
harperl-lgtm authored Dec 7, 2022
1 parent ef6ead4 commit a570fd2
Show file tree
Hide file tree
Showing 86 changed files with 432 additions and 81 deletions.
7 changes: 5 additions & 2 deletions packages/flutter/lib/src/material/calendar_date_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,10 @@ class _CalendarDatePickerState extends State<CalendarDatePicker> {
_textDirection = Directionality.of(context);
if (!_announcedInitialDate) {
_announcedInitialDate = true;
final bool isToday = DateUtils.isSameDay(widget.currentDate, _selectedDate);
final String semanticLabelSuffix = isToday ? ', ${_localizations.currentDateLabel}' : '';
SemanticsService.announce(
_localizations.formatFullDate(_selectedDate),
'${_localizations.formatFullDate(_selectedDate)}$semanticLabelSuffix',
_textDirection,
);
}
Expand Down Expand Up @@ -968,6 +970,7 @@ class _DayPickerState extends State<_DayPicker> {
(widget.selectableDayPredicate != null && !widget.selectableDayPredicate!(dayToBuild));
final bool isSelectedDay = DateUtils.isSameDay(widget.selectedDate, dayToBuild);
final bool isToday = DateUtils.isSameDay(widget.currentDate, dayToBuild);
final String semanticLabelSuffix = isToday ? ', ${localizations.currentDateLabel}' : '';

BoxDecoration? decoration;
Color dayColor = enabledDayColor;
Expand Down Expand Up @@ -1019,7 +1022,7 @@ class _DayPickerState extends State<_DayPicker> {
// day of month before the rest of the date, as they are looking
// for the day of month. To do that we prepend day of month to the
// formatted full date.
label: '${localizations.formatDecimal(day)}, ${localizations.formatFullDate(dayToBuild)}',
label: '${localizations.formatDecimal(day)}, ${localizations.formatFullDate(dayToBuild)}$semanticLabelSuffix',
selected: isSelectedDay,
excludeSemantics: true,
child: dayWidget,
Expand Down
3 changes: 2 additions & 1 deletion packages/flutter/lib/src/material/date_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2348,7 +2348,8 @@ class _MonthItemState extends State<_MonthItem> {
// day of month before the rest of the date, as they are looking
// for the day of month. To do that we prepend day of month to the
// formatted full date.
String semanticLabel = '${localizations.formatDecimal(day)}, ${localizations.formatFullDate(dayToBuild)}';
final String semanticLabelSuffix = DateUtils.isSameDay(widget.currentDate, dayToBuild) ? ', ${localizations.currentDateLabel}' : '';
String semanticLabel = '${localizations.formatDecimal(day)}, ${localizations.formatFullDate(dayToBuild)}$semanticLabelSuffix';
if (isSelectedDayStart) {
semanticLabel = localizations.dateRangeStartDateSemanticLabel(semanticLabel);
} else if (isSelectedDayEnd) {
Expand Down
6 changes: 6 additions & 0 deletions packages/flutter/lib/src/material/material_localizations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ abstract class MaterialLocalizations {
/// as a hint text in the text field.
String get searchFieldLabel;

/// Label indicating that a given date is the current date.
String get currentDateLabel;

/// The format used to lay out the time picker.
///
/// The documentation for [TimeOfDayFormat] enum values provides details on
Expand Down Expand Up @@ -1015,6 +1018,9 @@ class DefaultMaterialLocalizations implements MaterialLocalizations {
@override
String get searchFieldLabel => 'Search';

@override
String get currentDateLabel => 'Today';

@override
String aboutListTileTitle(String applicationName) => 'About $applicationName';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ void main() {
isFocusable: true,
));
expect(tester.getSemantics(find.text('3')), matchesSemantics(
label: '3, Sunday, January 3, 2016',
label: '3, Sunday, January 3, 2016, Today',
hasTapAction: true,
isFocusable: true,
));
Expand Down
6 changes: 6 additions & 0 deletions packages/flutter/test/material/date_picker_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,12 @@ void main() {
label: 'SELECT DATE\nFri, Jan 15',
));

expect(tester.getSemantics(find.text('3')), matchesSemantics(
label: '3, Sunday, January 3, 2016, Today',
hasTapAction: true,
isFocusable: true,
));

// Input mode toggle button
expect(tester.getSemantics(switchToInputIcon), matchesSemantics(
tooltip: 'Switch to input',
Expand Down
18 changes: 18 additions & 0 deletions packages/flutter/test/material/date_range_picker_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,24 @@ void main() {
expect(tester.getBottomRight(find.byType(DateRangePickerDialog)), const Offset(390.0, 600.0));
});
});

group('Semantics', () {
testWidgets('calendar mode', (WidgetTester tester) async {
final SemanticsHandle semantics = tester.ensureSemantics();
currentDate = DateTime(2016, DateTime.january, 30);
addTearDown(semantics.dispose);
await preparePicker(tester, (Future<DateTimeRange?> range) async {
expect(
tester.getSemantics(find.text('30')),
matchesSemantics(
label: '30, Saturday, January 30, 2016, Today',
hasTapAction: true,
isFocusable: true,
),
);
});
});
});
}

class _RestorableDateRangePickerDialogTestWidget extends StatefulWidget {
Expand Down
1 change: 1 addition & 0 deletions packages/flutter/test/material/localizations_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ void main() {
expect(localizations.keyboardKeyScrollLock, isNotNull);
expect(localizations.keyboardKeySelect, isNotNull);
expect(localizations.keyboardKeySpace, isNotNull);
expect(localizations.currentDateLabel, isNotNull);

expect(localizations.aboutListTileTitle('FOO'), isNotNull);
expect(localizations.aboutListTileTitle('FOO'), contains('FOO'));
Expand Down
Loading

0 comments on commit a570fd2

Please sign in to comment.