【笔记】flutter 日历年月日自定义国际化显示

问题:

在使用flutter 自动的日历选择组件CupertinoDatePicker的时候,flutter默认帮助我们做了国际化显示, 比如us ,年月日,月份是January...

而有时候我们想使用US,但又不想使用单词,我查看了官方API文档没有找到相关开放的配置。

bash 复制代码
    'January',
    'February',
    'March',
    'April',
    'May',
    'June',
    'July',
    'August',
    'September',
    'October',
    'November',
    'December',
解决:

经过分析国际化配置CupertinoLocalizations,发现有DefaultCupertinoLocalizationsGlobalCupertinoLocalizations CupertinoLocalizationAm 相关抽象类和实现类,

所以解决方法应运而生,就是extends这个默认实现类,重写想要的返回值,经过实践是可行,

下面粘贴一下相关代码,供大家参考:

源码
bash 复制代码
先建一个实现类,MyDefaultCupertinoLocalizations.dart
复制代码
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';

class MyDefaultCupertinoLocalizations extends DefaultCupertinoLocalizations {
  static const List<String> _months = <String>[
    '01',
    '02',
    '03',
    '04',
    '05',
    '06',
    '07',
    '08',
    '09',
    '10',
    '11',
    '12',
  ];

  @override
  String datePickerMonth(int monthIndex) => _months[monthIndex - 1];

  static Future<CupertinoLocalizations> load(Locale locale) {
    return SynchronousFuture<CupertinoLocalizations>(
        MyDefaultCupertinoLocalizations());
  }

  static const LocalizationsDelegate<CupertinoLocalizations> delegate =
  _CupertinoLocalizationsDelegate();
}

class _CupertinoLocalizationsDelegate
    extends LocalizationsDelegate<CupertinoLocalizations> {
  const _CupertinoLocalizationsDelegate();

  @override
  bool isSupported(Locale locale) => locale.languageCode == 'en';

  @override
  Future<CupertinoLocalizations> load(Locale locale) =>
      MyDefaultCupertinoLocalizations.load(locale);

  @override
  bool shouldReload(_CupertinoLocalizationsDelegate old) => false;

  @override
  String toString() => 'MyDefaultCupertinoLocalizations.delegate(en_US)';
}

在日历组件里配置这个类,就可以了,本文也到此结束,有问题可评论区讨论。

bash 复制代码
Localizations.override(
                context: context,
                locale: const Locale('en'),
                delegates: [ MyDefaultCupertinoLocalizations.delegate,
                ],
                child: CupertinoDatePicker(
                  initialDateTime: initialDateTime,
                  minimumYear: minimumYear,
                  maximumYear: maximumYear,
                  maximumDate: maximumDate,
                  minimumDate: minimumDate,
                  dateOrder: DatePickerDateOrder.dmy,
                  mode: CupertinoDatePickerMode.date,
                  onDateTimeChanged: (newDateTimes) {
                    newDateTime = newDateTimes;
                  },
                ),
              ),
相关推荐
月光下的丝瓜21 小时前
Flutter 国内安装指南
前端·flutter
恋猫de小郭3 天前
Amper 正式转正 Kotlin Toolchain ,Gradle 未来何去何从
android·前端·flutter
张风捷特烈3 天前
Flutter 类库大揭秘#02 | path_provider 各平台实现
前端·flutter
TT_Close4 天前
别劝退了!5秒搞定 Flutter 鸿蒙 FVM 起跑线
flutter·harmonyos·visual studio code
RainCity4 天前
Java Swing 自定义组件库分享(十二)
java·笔记·后端
你听得到114 天前
用户说 App 卡,但说不清在哪?我把 Flutter 监控 SDK 升级成了链路观测工作台
前端·flutter·性能优化
stringwu6 天前
Flutter 开发必备:MVI 架构的高效实现指南
前端·flutter
程序员老刘7 天前
Flutter版本选择指南:3.44系列继续观望 | 2026年6月
flutter·ai编程·客户端
牛奶7 天前
如何自己写一个浏览器插件?
前端·chrome·浏览器
用户965597361908 天前
Provider vs Bloc vs GetX vs Riverpod:Flutter 状态管理方案怎么选?
flutter