flutter - Flutter:将整数转换为月份
问题描述
我有模型和虚拟数据要像这样进行测试:
模型
class TestingLoopingModel {
String id;
int month;
int year;
int value;
TestingLoopingModel({
this.id,
this.month,
this.year,
this.value,
});
}
虚拟数据
List<TestingLoopingModel> listTest = [
//TODO Month 12 And 10 is not exist
TestingLoopingModel(
id: DateTime.now().toString(),
month: 2,
year: 2020,
value: 2000,
),
TestingLoopingModel(
id: DateTime.now().toString(),
month: 4,
year: 2020,
value: 1500,
),
TestingLoopingModel(
id: DateTime.now().toString(),
month: 6,
year: 2020,
value: 3000,
),
TestingLoopingModel(
id: DateTime.now().toString(),
month: 8,
year: 2020,
value: 3500,
),
TestingLoopingModel(
id: DateTime.now().toString(),
month: 1,
year: 2020,
value: 5000,
),
TestingLoopingModel(
id: DateTime.now().toString(),
month: 3,
year: 2020,
value: 4500,
),
TestingLoopingModel(
id: DateTime.now().toString(),
month: 5,
year: 2020,
value: 2111,
),
TestingLoopingModel(
id: DateTime.now().toString(),
month: 7,
year: 2020,
value: 5555,
),
TestingLoopingModel(
id: DateTime.now().toString(),
month: 9,
year: 2020,
value: 333,
),
TestingLoopingModel(
id: DateTime.now().toString(),
month: 11,
year: 2020,
value: 2222,
),
];
我希望我的 listTest 中的 Month 转换为 Month Name Something Like January ,Feburary,March,April,Mei,etc ...。我怎样才能做到这一点 ?
我知道我可以像这样
使用这个Intl PackagesDateFormat.MMMM().format(???);
但是格式需要 DateTime 并且在我的列表中 Month 是整数。
谢谢
看法
SizedBox(
height: sizes.height(context) / 1.7,
child: ListView.separated(
separatorBuilder: (ctx, index) => Divider(),
itemCount: 12,
shrinkWrap: true,
itemBuilder: (BuildContext context, int index) {
final indexOrder = index + 1;
final result = listTest[index];
listTest.sort((a, b) => a.month.compareTo(b.month));
return InkWell(
onTap: () => showDialog(
context: context,
builder: (ctxDialog) => FormElectricTenant(),
),
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: Row(
children: <Widget>[
titleHeaderRow(title: '$indexOrder' ?? '0'),
titleHeaderRow(title: '${result.month}' ?? '0'),
titleHeaderRow(title: '${result.value}' ?? '0'),
],
),
),
);
},
),
),
解决方案
DateFormat.MMMM().format(date)
简单并允许未来本地化。您可以将您的语言环境放在.MMMM()
. 这是最好的和最具可扩展性的解决方案。
推荐阅读
- python - 来自 scipy 的 jarque_bera 计算
- android - KeyEvents在android中的文本之后没有占用空间
- c# - 使用自定义方法结合 Where 和 OrderByDescending
- sql - 这是一个 Sql-Server 查询,如何在 Postgresql 中执行?
- java - Quarkus 和 Redis
- java - Why Selection event not triggered when selection is done using keyboard?
- django - 如何在我的博客中使用 ManyToManyField 作为标签
- c# - 具有依赖注入的 EF 核心 DbContext
- c++ - 是否可以在 c++ 中使用 for 循环遍历一对值?
- c# - 我如何保护 c# dll?