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` (#416)

* fix mangled conflict resolution part 1

* fix mangled PR conflict resolution part 2

* fix: remove missed state property

---------

Co-authored-by: GroovinChip <groovinchip@gmail.com>
  • Loading branch information
rklos and GroovinChip authored Jul 4, 2023
1 parent 37db0c3 commit 044af12
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 45 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## [2.0.0-beta.3]
✨ New ✨
* Added support for `weekdayAbbreviations` and `monthAbbreviations` to `MacosDatePicker`.
* Added support for `dateFormat` to `MacosDatePicker`.
* Added support for `startWeekOnMonday` to `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

You can also define the `dateFormat` to change the way dates are displayed in the textual interface.
It takes a string of tokens (case-insensitive) and replaces them with their corresponding values.
The following tokens are supported:
Expand Down
47 changes: 33 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',
],
this.dateFormat,
this.startWeekOnMonday,
});
Expand All @@ -61,6 +86,12 @@ class MacosDatePicker extends StatefulWidget {
/// Defaults to `DateTime.now()`.
final DateTime? initialDate;

/// A list of 7 strings, one for each day of the week, starting with Sunday.
final List<String> weekdayAbbreviations;

/// A list of 12 strings, one for each month of the year, starting with January.
final List<String> monthAbbreviations;

/// Changes the way dates are displayed in the textual interface.
///
/// The following tokens are supported (case-insensitive):
Expand All @@ -87,18 +118,6 @@ class MacosDatePicker extends StatefulWidget {
}

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 @@ -193,7 +212,7 @@ class _MacosDatePickerState extends State<MacosDatePicker> {
}

for (int i = firstDayOfWeekIndex; result.length < 7; i = (i + 1) % 7) {
final weekday = _narrowWeekdays[i];
final weekday = widget.weekdayAbbreviations[i];
result.add(
ExcludeSemantics(
child: Center(
Expand Down Expand Up @@ -398,7 +417,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 @@ -360,6 +360,66 @@ 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);
},
);
});

testWidgets(
Expand Down

0 comments on commit 044af12

Please sign in to comment.