【问题标题】:GridView inside Expanded not visible, only visible when using Container with a specified heightExpanded 内的GridView 不可见,仅在使用指定高度的Container 时可见
【发布时间】:2020-12-10 11:44:29
【问题描述】:

我有一个网格视图,我想在非特定高度容器中显示它。但是,只有当我使用具有指定高度的容器时,才会显示网格视图。当我将 Container 更改为 Expanded 或 Flexible 时,GridView 变得不可见。

工作代码:

return Container( //using container
  height: 250,    //with a specific height
  child: GridView.count(
    primary: false,
    padding: const EdgeInsets.all(0.0),
    crossAxisSpacing: 10.0,
    crossAxisCount: 2,
    children: <Widget>[
      const Text('He\'d have you all unravel at the'),
      const Text('Heed not the rabble'),
      const Text('Sound of screams but the'),
      const Text('Who scream'),
    ],
  ),
);

非工作代码:

return Expanded(
  child: GridView.count(
    primary: false,
    padding: const EdgeInsets.all(0.0),
    crossAxisSpacing: 10.0,
    crossAxisCount: 2,
    children: <Widget>[
      const Text('He\'d have you all unravel at the'),
      const Text('Heed not the rabble'),
      const Text('Sound of screams but the'),
      const Text('Who scream'),
    ],
  ),
);

【问题讨论】:

    标签: flutter gridview flutter-layout


    【解决方案1】:

    gridview的父widget需要有一定的高度。 height: MediaQuery.of(context).size.height 如果容器不可滚动或已发布类似答案,则为选项之一,请参阅link

    【讨论】:

    • 这将占据整个屏幕。我的 gridview 只是我屏幕上的一个部分。我不希望它占据整个屏幕的高度,而是让它在需要时扩展。
    • 尝试使用return SizedBox.expand(child: Container(child: GridView.count(
    【解决方案2】:

    你必须扩展成一个 Column Widget,它会占用所有可用的垂直空间

    return Column(
        children:<Widget>[
          Expanded(
            child: GridView.count(
              primary: false,
              padding: const EdgeInsets.all(0.0),
              crossAxisSpacing: 10.0,
              crossAxisCount: 2,
              children: <Widget>[
                const Text('He\'d have you all unravel at the'),
                const Text('Heed not the rabble'),
                const Text('Sound of screams but the'),
                const Text('Who scream'),
              ],
            ),
          )
        ]
    );
    

    【讨论】:

    • 还是不行。整个事情都被屏蔽了。
    • @NewbieCoder 您是否将此小部件包装到另一个小部件(可能是中心)中?我的意思是,我的树是Scaffold(...Column(...Expanded(...GridView.count(...))))
    猜你喜欢
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多