我想向用户显示一周中某天的本地化列表(用于设置重复事件)。我可以看到MaterialLocalizations类具有一个narrowWeekdays
列表,您可以将其与firstDayOfWeekIndex
属性一起使用,但是narrowWeekdays
只是1或2个字母(取决于语言环境) )。
令人讨厌的是,该实现似乎有一个_weekdays
列表,但仅用于格式化长日期,并且不像窄名那样公开:(
有什么想法吗?
我已经“黑客入侵”了以下内容,但似乎应该容易得多:
List<String> _getLocalizedDayNames(BuildContext context) {
var localizations = MaterialLocalizations.of(context);
// 6th August 2018 is a Monday.
final DateFormat longDayName = DateFormat.EEEE();
final oneDay = Duration(days: 1);
final DateTime monday = DateTime.utc(2018, DateTime.august, 6);
final DateTime tuesday = monday.add(oneDay);
final DateTime wednesday = tuesday.add(oneDay);
final DateTime thursday = wednesday.add(oneDay);
final DateTime friday = thursday.add(oneDay);
final DateTime saturday = friday.add(oneDay);
final DateTime sunday = saturday.add(oneDay);
final weekdays = <String>[
longDayName.format(monday),
longDayName.format(tuesday),
longDayName.format(wednesday),
longDayName.format(thursday),
longDayName.format(friday),
longDayName.format(saturday),
];
if (localizations.firstDayOfWeekIndex == 0) {
// Sunday is the first day of the week.
weekdays.insert(0, longDayName.format(sunday));
} else {
weekdays.add(longDayName.format(sunday));
}
return weekdays;
}