You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter:如何修改simple_month_year_picker控件的语言?

解决simple_month_year_picker控件语言修改问题

当前你使用的SimpleMonthYearPicker默认显示英文,可通过以下两种方式修改为中文显示:

方法一:直接指定Locale参数

调用showMonthYearPickerDialog时添加locale参数,传入中文区域配置:

final selectedDate = await SimpleMonthYearPicker.showMonthYearPickerDialog(
  context: context,
  disableFuture: true,
  locale: const Locale('zh', 'CN'), // 设置中文显示
);

方法二:自定义月份/年份文本

如果控件对Locale支持有限,可通过自定义构建器实现中文显示:

  1. 先定义中文月份列表:
const List<String> chineseMonths = [
  '一月', '二月', '三月', '四月', '五月', '六月',
  '七月', '八月', '九月', '十月', '十一月', '十二月'
];
  1. 调用picker时替换默认的月份、年份渲染逻辑:
final selectedDate = await SimpleMonthYearPicker.showMonthYearPickerDialog(
  context: context,
  disableFuture: true,
  monthBuilder: (context, month, isSelected) {
    return Text(
      chineseMonths[month - 1],
      style: TextStyle(
        color: isSelected ? Colors.blue : Colors.black,
        fontSize: 16,
      ),
    );
  },
  yearBuilder: (context, year, isSelected) {
    return Text(
      '$year年',
      style: TextStyle(
        color: isSelected ? Colors.blue : Colors.black,
        fontSize: 16,
      ),
    );
  },
);

另外,格式化选中日期时,也要同步指定中文Locale,确保月份显示为中文:

String pickedMonth = DateFormat('MMMM yyyy', 'zh_CN').format(selectedDate);

内容的提问来源于stack exchange,提问作者jose andres inga medina

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 23:57:41