在Flutter应用中,intl包仅加载15 languages。但是Dart DateFormat支持更多。我不需要翻译,但需要正确--mirror
。如何加载所有DateFormat
区域设置?
答案 0 :(得分:7)
在最高的StatefulWidget中添加这些导入
import 'package:intl/intl.dart';
import 'package:intl/date_symbol_data_local.dart';
在其状态中,覆盖initState并添加
@override
void initState() {
super.initState();
initializeDateFormatting();
}
例如
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:intl/date_symbol_data_local.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Intl Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Intl Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
DateFormat dateFormat;
DateFormat timeFormat;
@override
void initState() {
super.initState();
initializeDateFormatting();
dateFormat = new DateFormat.yMMMMd('cs');
timeFormat = new DateFormat.Hms('cs');
}
void _refresh() {
setState(() {});
}
@override
Widget build(BuildContext context) {
var dateTime = new DateTime.now();
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(dateFormat.format(dateTime)),
new Text(timeFormat.format(dateTime)),
],
),
),
floatingActionButton: new FloatingActionButton(
onPressed: _refresh,
tooltip: 'Refresh',
child: new Icon(Icons.refresh),
),
);
}
}
Dart主要使用ISO 631 2个字母代码,如果适用,则使用国家/地区变体。例如:ja, en_US, en_GB, zh_HK, zh_CN
。偶尔它使用3个字母的代码。要查找完整列表,只需在文件date_symbol_data_local.dart
DateSymbols
即可
答案 1 :(得分:2)
导入
import 'package:intl/intl.dart'; //for date format
import 'package:intl/date_symbol_data_local.dart'; //for date locale
并在初始化DateFormat时使用当前languageCode
String languageCode = Localizations.localeOf(context).languageCode;
String dateTime = DateFormat( "dd.MM.yy HH:mm", languageCode).format(this.date);
答案 2 :(得分:1)
Flutter中所有可用语言环境的列表可以来自顶级属性availableLocalesForDateFormatting