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

问题:

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

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

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

经过分析国际化配置CupertinoLocalizations,发现有DefaultCupertinoLocalizations和 GlobalCupertinoLocalizations 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;
                  },
                ),
              ),
相关推荐
程序员老刘9 小时前
Flutter版本选择指南:尽早升级3.47.9 | 2026年11月
flutter·ai编程·客户端
Frag0ut14 小时前
Chrome历史记录与会话恢复全攻略
chrome·谷歌浏览器·效率工具·历史记录·会话恢复·数据找回·最近关闭的窗口
小飞在努力15 小时前
【实现负载均匀在线判题⑤】MVC 架构搭建与 Model 题目数据模块实现
c语言·开发语言·c++·笔记·后端
叼烟扛炮17 小时前
[LangChain] 学习笔记(24):流式不改总耗时,只改「多久看到第一个字」——实测差 3 倍
笔记·学习·langchain
恋猫de小郭18 小时前
Android Keep Rules 解读,直接影响 Google Play 上架和曝光
android·前端·flutter
小飞在努力18 小时前
【实现负载均匀在线判题④】接入 cpp-httplib 实现编译服务 HTTP 网络化
c语言·开发语言·c++·笔记·http
蜡笔小谢1 天前
DeepSeek Harness Java 实战笔记(五)|插件、MCP 与 Skill
java·笔记·ai·agent·deepseek·harness
琳少_1 天前
java程序设计笔记
java·开发语言·数据库·笔记
摇滚侠2 天前
《On Java 中文版 基础卷》阅读笔记 接口 19
java·开发语言·笔记
虎符饼干2 天前
用WebHook把AI文章推送到自建官网,接收端需要具备哪些处理与发布能力?
笔记