gpt4 book ai didi

flutter - Flutter:将整数转换为月

转载 作者:行者123 更新时间:2023-12-03 03:36:22 26 4
gpt4 key购买 nike

我有模型和虚拟数据要进行这样的测试:

模型


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,
),
];


我想在列表测试中将 转换为月份名称类似于 一月,二月,三月,四月,梅等... 。我怎样才能做到这一点 ?
我知道我可以这样使用 Intl Packages DateFormat.MMMM().format(???);但是格式是必需的DateTime,在我的列表中Month是整数。

谢谢

View
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'),
],
),
),
);
},
),
),

enter image description here

最佳答案

const Map<int,String> monthsInYear = {
1: "January",
2: "February",
3: "March",
4: “April”,
5: “May”,
6: “June”

12: "December"
}

在您的ListView中
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: '${monthsInYear[result.month]}' ?? '---'),
titleHeaderRow(title: '${result.value}' ?? '0'),
],
),
),
);
},
);

关于flutter - Flutter:将整数转换为月,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60881936/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com