【问题标题】:Is there a way to get fixed size for child inside GridView?有没有办法在 GridView 中为孩子获得固定大小?
【发布时间】:2021-12-17 01:16:02
【问题描述】:

有没有办法为GridView 中的孩子设置固定大小?我想让红色的高度相同。我尝试使用Expanded 或指定Container 的大小,但它不起作用。 GridView 只是让孩子大小相同。

注意:我尝试指定红色容器的高度。没用。

这就是我得到的。

这就是我所做的。

    return GridView.count(
      crossAxisCount: 2,
      children: List.generate(drinksData.length, (index) {
        return Container(
          padding: EdgeInsets.all(20.0),
          child: Column(
            children: [
              Expanded(
                child: Container(
                  color: Colors.red,
                ),
              ),
              Text(drinksData[index].label),
            ],
          ),
        );
      }),
    );

【问题讨论】:

  • 是的!只需在容器中添加height: 100
  • 嘿戴维斯,感谢您的回复,但我尝试指定高度。它不起作用。 GridView 只是让它对孩子来说是平等的。你可以用代码试试看。
  • 检查更新的代码!

标签: flutter dart user-interface gridview


【解决方案1】:
GridView.count(
     crossAxisCount: 2,
     children: List.generate(drinksData.length, (index) {
       return Container(
         padding: const EdgeInsets.all(20.0),
         child: Column(
           children: [
             Expanded(
               child: Container(
                // height:90,//or whatever size you want!
                 color: Colors.red,
               ),
             ),
             SizedBox(
               height: 45,
                 child: Text(drinksData[index])
             ),
           ],
         ),
       );
     }),
   )
   ```

【讨论】:

  • 第一!让它无缝!知道文本的最大高度,然后在 sizedbox 中提供它,并在文本推翻高度的情况下处理溢出行为!
  • 啊。这有帮助,非常感谢戴维斯!祝你有美好的一天!
  • 最受欢迎!!!
猜你喜欢
  • 2019-08-20
  • 2020-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-18
  • 1970-01-01
  • 1970-01-01
  • 2020-02-24
相关资源
最近更新 更多