1

我正在为我的应用程序编写代码,但是代码的一部分给我带来了问题。特别是这个:

  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 小部件分别滚动,但到目前为止我还没有成功。有谁知道为什么?

编辑:两个答案一起解决了这个问题。

4

2 回答 2

3

使用此小部件包装 Column:

SingleChildScrollView

之后应该是这样的

SingleChildScrollView(
    child: Column(
     )
     )

这将使列可滚动

于 2021-07-16T13:22:54.147 回答
2

完成@Jesus Loves You 方法,添加NeverScrollableScrollPhysics到 GridView:

SingleChildScrollView(
      child: Column(
        children: <Widget>[
          GridView.count(
            physics: const NeverScrollableScrollPhysics(), //Here
            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'),
            ],
          ),
        ],
      ),
    ),
于 2021-07-19T13:44:33.133 回答