[英]Using an algorithm to generate days in a month
我对 Flutter 和一般编程很陌生,我正在做这个迷你项目,但我被困住了。
我已经创建了一年中月份的列表,并且我想在用户点击一个月时生成相应月份的天数。
我产生这些日子的想法很简单。 当我创建月份列表时,我使用了 class 和构造函数。 此构造函数将询问月份和月份的编号。 一月有数字“1”,二月有“2”到十二月有“12”
因此,我计划创建一个从一到三十一(一个月中的天数)的 for 循环。 根据与月份相关的数字,它将计数到 28,30 或 31。 因此,如果用户在 1 月点击,它必须计数到 31 天并生成天数列表,如果是 2 月则为 28 天。
正如您将从我的代码中看到的那样,我已经减少了月份部分,但生成日期是一个问题。
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(home: home(), title: 'Flutter Demo', theme: ThemeData());
}
}
class home extends StatefulWidget {
@override
_homeState createState() => _homeState();
}
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),
];
Widget monthtemp(display) {
return FlatButton(
child: Card(
child: Text(display.month),
margin: EdgeInsets.all(20),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Column(
children:
monthoftheyear.map((display) => monthtemp(display)).toList()),
),
);
}
}
for (int i = 0; i>= 31; i++);
如果(月份 id 为 1,那么 for 循环应该最多计数 31 并将其放入卡片中,如果它是 2 月,那么它应该最多计数 28 等)
随意使用搜索 function: 这个线程已经提供了一些部分解决方案。 我认为这样的事情应该有效:
// look up number of days for this month in a small lookup table
var nrOfDays = ...;
// generate a list with all days
var days = Iterable<int>.generate(nrOfDays + 1).skip(1).toList();
// do something with your days to display them
var displayedDays = days.map((day) => ...);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.