Skip to content

Commit

Permalink
feat: Added support for weekdayAbbreviations and `monthAbbreviation…
Browse files Browse the repository at this point in the history
…s` to `MacosDatePicker`
  • Loading branch information
rklos committed Jul 3, 2023
1 parent 46eb9ca commit 81eacfc
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 45 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## [2.0.0-beta.3]
✨ New ✨
* Added support for `weekdayAbbreviations` and `monthAbbreviations` to `MacosDatePicker`.

🛠️ Fixed 🛠️
* Better UX of the click on the calendar elements in `MacosDatePicker`

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,10 @@ There are three styles of `MacosDatePickers`:
calendar-like interface to select a date.
* `combined`: provides both `textual` and `graphical` interfaces.

Localization of the time picker is supported by the `weekdayAbbreviations` and `monthAbbreviations` parameters (instead of e.g. standard `localizations.narrowWeekdays()` in order to match Apple's spec).
* `weekdayAbbreviations` should be a list of 7 strings, one for each day of the week, starting with Sunday
* `monthAbbreviations` should be a list of 12 strings, one for each month of the year, starting with January

Example usage:
```dart
MacosDatePicker(
Expand Down
46 changes: 32 additions & 14 deletions lib/src/selectors/date_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,31 @@ class MacosDatePicker extends StatefulWidget {
this.style = DatePickerStyle.combined,
required this.onDateChanged,
this.initialDate,
// Use this to get the weekday abbreviations instead of
// localizations.narrowWeekdays() in order to match Apple's spec
this.weekdayAbbreviations = const [
'Su',
'Mo',
'Tu',
'We',
'Th',
'Fr',
'Sa',
],
this.monthAbbreviations = const [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
],
});

/// The [DatePickerStyle] to use.
Expand All @@ -59,23 +84,16 @@ class MacosDatePicker extends StatefulWidget {
/// Defaults to `DateTime.now()`.
final DateTime? initialDate;

/// Should be a list of 7 strings, one for each day of the week, starting with Sunday
final List<String> weekdayAbbreviations;
/// Should be a list of 12 strings, one for each month of the year, starting with January
final List<String> monthAbbreviations;

@override
State<MacosDatePicker> createState() => _MacosDatePickerState();
}

class _MacosDatePickerState extends State<MacosDatePicker> {
// Use this to get the weekday abbreviations instead of
// localizations.narrowWeekdays() in order to match Apple's spec
static const List<String> _narrowWeekdays = <String>[
'Su',
'Mo',
'Tu',
'We',
'Th',
'Fr',
'Sa',
];

final _today = DateTime.now();
late final _initialDate = widget.initialDate ?? _today;

Expand Down Expand Up @@ -159,7 +177,7 @@ class _MacosDatePickerState extends State<MacosDatePicker> {
) {
final result = <Widget>[];
for (int i = localizations.firstDayOfWeekIndex; true; i = (i + 1) % 7) {
final weekday = _narrowWeekdays[i];
final weekday = widget.weekdayAbbreviations[i];
result.add(
ExcludeSemantics(
child: Center(
Expand Down Expand Up @@ -326,7 +344,7 @@ class _MacosDatePickerState extends State<MacosDatePicker> {
children: [
Expanded(
child: Text(
'${intToMonthAbbr(_selectedMonth)} $_selectedYear',
'${widget.monthAbbreviations[_selectedMonth - 1]} $_selectedYear',
style: const TextStyle(
fontSize: 13.0,
fontWeight: FontWeight.w700,
Expand Down
31 changes: 0 additions & 31 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,37 +59,6 @@ Color iconLuminance(Color backgroundColor, bool isDark) {
}
}

String intToMonthAbbr(int month) {
switch (month) {
case 1:
return 'Jan';
case 2:
return 'Feb';
case 3:
return 'Mar';
case 4:
return 'Apr';
case 5:
return 'May';
case 6:
return 'Jun';
case 7:
return 'Jul';
case 8:
return 'Aug';
case 9:
return 'Sep';
case 10:
return 'Oct';
case 11:
return 'Nov';
case 12:
return 'Dec';
default:
throw Exception('Unsupported value');
}
}

class Unsupported {
const Unsupported(this.message);

Expand Down
60 changes: 60 additions & 0 deletions test/selectors/date_picker_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -307,5 +307,65 @@ void main() {
}
},
);

testWidgets(
'Graphical MacosDatePicker renders abbreviations based on "weekdayAbbreviations" and "monthAbbreviations" properties',
(tester) async {
await tester.pumpWidget(
MacosApp(
home: MacosWindow(
child: MacosScaffold(
children: [
ContentArea(
builder: (context, _) {
return Center(
child: MacosDatePicker(
initialDate: DateTime.parse('2023-04-01'),
onDateChanged: (date) {},
weekdayAbbreviations: const [
'Nd',
'Po',
'Wt',
'Śr',
'Cz',
'Pt',
'So',
],
monthAbbreviations: const [
'Sty',
'Lut',
'Mar',
'Kwi',
'Maj',
'Cze',
'Lip',
'Sie',
'Wrz',
'Paź',
'Lis',
'Gru',
],
),
);
},
),
],
),
),
),
);

await tester.pumpAndSettle();

expect(find.text('Kwi 2023'), findsOneWidget);
expect(find.text('Nd'), findsOneWidget);
expect(find.text('Po'), findsOneWidget);
expect(find.text('Wt'), findsOneWidget);
expect(find.text('Śr'), findsOneWidget);
expect(find.text('Cz'), findsOneWidget);
expect(find.text('Pt'), findsOneWidget);
expect(find.text('So'), findsOneWidget);
},
);
});
}

0 comments on commit 81eacfc

Please sign in to comment.