簡體   English   中英

一個容器如何完美的包裹一個gridview flutter

[英]How can a container perfectly wrap a gridview flutter

我正在創建一個應用程序,我需要在其中使用 GridView。 它不適用於return GridView所以我不得不用容器包裝它。 問題是,如果我沒有為容器定義精確的高度,代碼將不起作用。 所以我希望 Container 能夠完美匹配 GridView 的大小,無論它的高度是多少。 我嘗試使用 Wrap 小部件但沒有成功。 你能幫我嗎?

return Container(
      //height: 10000, I want the height to adapt with the GridView
      child: GridView.count(
          scrollDirection: Axis.vertical,
          crossAxisCount: 2,
          children: List.generate(List.length, (index) {
            return Padding(
              padding: const EdgeInsets.all(8.0),
              child: Container(
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(20),
                  color: Colors.black,
                  // image: [index].img,
                  boxShadow: [
                    BoxShadow(
                      color: Colors.grey.withOpacity(0.8),
                      spreadRadius: 2,
                      blurRadius: 2,
                      offset: Offset(0, 3), // changes position of shadow
                    ),
                  ],
                ),
              ),
            );
          })),
    );

如果沒有其余的代碼,很難知道是什么導致了這種情況。

但是,我建議您將GridView小部件放入擴展小部件中。

這樣,您就不需要用 Container 包裝 GridView。

Expanded(
    child:GridView.count(
      scrollDirection: Axis.vertical,
      crossAxisCount: 2,
      children: List.generate(4, (index) {
        return Padding(
          padding: const EdgeInsets.all(8.0),
          child: Container(
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(20),
              color: Colors.black,
              // image: [index].img,
              boxShadow: [
                BoxShadow(
                  color: Colors.grey.withOpacity(0.8),
                  spreadRadius: 2,
                  blurRadius: 2,
                  offset: Offset(0, 3), // changes position of shadow
                ),
              ],
            ),
          ),
        );
      },
    ),
  ),
),

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM