【发布时间】:2021-04-12 00:21:12
【问题描述】:
我对 Flutter 还很陌生,目前正在研究这个小项目,以帮助我更好地理解使用 push.named 和 ModalRoute of 方法通过路由传递数据。
项目描述:
-
我想创建一个月份列表,然后在您点击它时创建该月中的日期列表。例如:当我点击 1 月时,必须列出 31 天,2 月 = 29 或 28,具体取决于是否是闰年。
-
我已经能够生成月份列表,但我的问题在于日期。
要求:我使用了一个类来生成一年中的月份,并为每个月份关联一个数字,如下所示:
List<Months> _monthoftheyear =[
Months(month: "January",id: 1 ),
Months(month: "February",id: 2),
Months(month: "March",id: 3),
Months(month: "April",id: 4),
Months(month: "May",id: 5),
Months(month: "June",id: 6),
Months(month: "July",id: 7),
Months(month: "August",id: 8),
Months(month: "September",id: 9),
Months(month: "October",id: 10),
Months(month: "November",id: 11),
Months(month: "December",id: 12),
];
我想要做的是在 switch 语句中使用id,表示对于案例 1、3、5(31 天的月份)应该有一个 31 天的列表
switch (Months.id){
case 1:
case 3:
case 5:
return List.generate(31,(count) "Day $count")
}
但我试图在我的 Push.named 的参数参数中执行此操作,然后提取它并生成它,就像我在它不工作的几个月中所做的那样。
谁能指出我正确的方向?我的完整代码如下
我的班级
class Months {
String month;
int id;
Months ({this.month, this.id});
}
**FIRST PAGE**
class _HomeState extends State<Home> {
List<Months> _monthoftheyear =[
Months(month: "January",id: 1 ),
Months(month: "February",id: 2),
Months(month: "March",id: 3),
Months(month: "April",id: 4),
Months(month: "May",id: 5),
Months(month: "June",id: 6),
Months(month: "July",id: 7),
Months(month: "August",id: 8),
Months(month: "September",id: 9),
Months(month: "October",id: 10),
Months(month: "November",id: 11),
Months(month: "December",id: 12),
];
@override
Widget build(BuildContext context) {
return Scaffold(
//Listview builder that returns the entirety of the months of the year in a list tile
body: ListView.builder(
itemCount: _monthoftheyear.length,
itemBuilder: (context, index){
return ListTile(
title: Text(_monthoftheyear[index].month),
onTap: (){
// the argument is that the id should be passed to the next page when the month is
tapped on
// THIS IS WHERE THE ISSUE IS. ALL I'M SEEING IS THE "ID" ITSELF AND NOT THE NUMBERS I WANT TO GENERATE. I'm NOT SURE OF WHAT TO DO SO PLEASE PARDON ME IF YOU GET OFFENDED IN ANYWAY
Navigator.pushNamed(context, "/days",arguments: Months (id:_monthoftheyear[index].id));
setState(() {
switch ( _monthoftheyear[index].id){
case 1:
List.generate(31, (index) => index);
print(_monthoftheyear[index].id);
break;
}
});
},
);
}
),
);
细节/摘录参数页面
class Days extends StatelessWidget {
@override
Widget build(BuildContext context) {
final Months _idinfo = ModalRoute.of(context).settings.arguments;
return Scaffold(
body:ListTile(
title: Text(_idinfo.id.toString()),
)
);
}
}
【问题讨论】: