我正在为我的应用程序编写代码,但是代码的一部分给我带来了问题。特别是这个:
List<Widget> _days() {
List<Widget> widgets = [];
for (int c = 0; c < 800; c++) {
widgets.add(InkWell(
child: Container(
padding: EdgeInsets.all(8),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: dateColors[c],
),
child: setDay(c),
),
onTap: () {
setState(() {
changeColors(c);
});
},
));
}
return widgets;
}
@override
Widget build(BuildContext context) {
List months = [
'JANUARY ',
'FEBRUARY ',
'MARCH ',
'APRIL ',
'MAY ',
'JUNE ',
'JULY ',
'AUGUST ',
'SEPTEMBER ',
'OCTOBER ',
'NOVEMBER ',
'DECEMBER '
];
int year = 2021;
String yearStr = year.toString();
return MaterialApp(
title: 'Calendar',
home: Scaffold(
appBar: AppBar(title: Center(child: Text(months[0] + yearStr))),
body: Column(
children: <Widget>[
GridView.count(
shrinkWrap: true,
crossAxisSpacing: 10,
mainAxisSpacing: 2,
crossAxisCount: 7,
children: _days(),
),
ListView(
shrinkWrap: true,
children: <Widget>[
Text("Event 1"),
Text('Event 2'),
Text('Event 3'),
Text('Event 4'),
],
),
],
)),
);
}
当我运行它时,它给了我这个:
如果我在 GridView 小部件中使用较少的天数(换句话说,for 循环中的迭代次数较少),则不会出现问题。我正在尝试使 GridView 小部件和 ListView 小部件分别滚动,但到目前为止我还没有成功。有谁知道为什么?
编辑:两个答案一起解决了这个问题。