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

如何将Flutter Cupertino DateTime Picker切换为捷克语?

解决CupertinoDatePicker捷克语本地化问题

要让CupertinoDatePicker显示捷克语,无需额外第三方包,用Flutter官方本地化工具即可实现,步骤如下:

1. 添加依赖

在pubspec.yaml中补充以下依赖:

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter
  intl: ^0.18.1 # 可选,用于格式化本地化日期文本

2. 配置App本地化代理

在你的MaterialApp或CupertinoApp中配置本地化参数:

import 'package:flutter_localizations/flutter_localizations.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      localizationsDelegates: const [
        GlobalCupertinoLocalizations.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
      ],
      supportedLocales: const [
        Locale('cs', 'CZ'), // 捷克语
        Locale('en', 'US'), // 备用英文
      ],
      locale: const Locale('cs', 'CZ'), // 可选:强制使用捷克语,不设置则跟随系统语言
      home: YourScreen(), // 你的目标页面
    );
  }
}

3. 优化日期显示文本

你当前代码中手动拼接的日期格式虽符合捷克习惯,但如果需要显示本地化的月份名称(比如“leden”而非数字1),可借助intl包的DateFormat:

import 'package:intl/intl.dart';
import 'package:intl/date_symbol_data_local.dart';

// 在页面初始化时加载捷克语日期数据
@override
void initState() {
  super.initState();
  initializeDateFormatting('cs_CZ', null);
}

// 格式化日期(示例)
Text(DateFormat.yMd('cs_CZ').format(whenDate), 
  style: TextStyle(color: Colors.grey.shade800, fontSize: 25, fontWeight: FontWeight.w400)
)
// 若需带月份名称的格式:DateFormat.MMMEd('cs_CZ').format(whenDate)

验证效果

配置完成后,CupertinoDatePicker的月份、星期等文本会自动切换为捷克语,比如月份显示为“leden”“únor”,星期显示为“pondělí”等。

说明:flutter_localizations官方已内置捷克语的Cupertino本地化支持,无需额外第三方包,只要正确配置代理和语言环境即可生效。

内容的提问来源于stack exchange,提问作者robstrai242

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 17:40:02