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支持有限,可通过自定义构建器实现中文显示:
- 先定义中文月份列表:
const List<String> chineseMonths = [ '一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月' ];
- 调用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
相关产品推荐
相关产品推荐

