簡體   English   中英

如何使 Flutter ListView 高度與子高度相同(動態)

[英]How to make Flutter ListView height same as children height(dynamic)

我想讓 ListView 與其內容具有相同的高度,
但總是得到水平視口被賦予無界高度錯誤
我試圖定義一個高度為 ListView 父級的 SizedBox ,但這似乎很奇怪
所有的項目都在下降
我如何在沒有定義高度的情況下定義這個 ListView(動態跟隨內容高度)?
使用listview前的結果
使用 ListView 后(奇怪的樣子)

有代碼:

Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              ...OTHER CHILD,
              ListView(
                scrollDirection: Axis.horizontal,
                children: [
                  HorizontalList(),
                  SizedBox(width: 10),
                  HorizontalList(),
                  SizedBox(width: 10),
                  HorizontalList(),
                ],
              ),
            ],
          ),

有子組件:

Container(
      width: 260,
      height: 210,
      decoration: BoxDecoration(
        color: whiteColor,
        borderRadius: BorderRadius.all(Radius.circular(14)),
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withOpacity(0.5),
            spreadRadius: 1,
            blurRadius: 5,
            offset: Offset(0, 0),
          ),
        ],
      ),
      child: Column(
        children: [
          ...OTHER CHILD,
        ],
      ),
    );

ListView中設置shrinkWrap: true並使用Flexible的小部件包裝ListView


Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              ...OTHER CHILD,
              Flexible(
              child: ListView(
                shrinkWrap: true,
                scrollDirection: Axis.horizontal,
                children: [
                  HorizontalList(),
                  SizedBox(width: 10),
                  HorizontalList(),
                  SizedBox(width: 10),
                  HorizontalList(),
                ],
              ),
             ),
            ],
          ),

這樣ListView將只占用孩子們需要的空間。

我想如果你用這個

 crossAxisAlignment: CrossAxisAlignment.stretch,

而不是這個

crossAxisAlignment: CrossAxisAlignment.start

那么它將起作用。

ListView將占用所有可用空間。

如果scrollDirection設置為Axis.horizontal它將占用所有可用的高度,或者如果它設置為verticle那么它將占用所有可用的寬度。 這是默認行為。

您可以將SingleChildScrollViewRow一起使用。

Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              ...OTHER CHILD,
              SingleChildScrollView(
                scrollDirection: Axis.horizontal,
                child: Row(
                 children: [
                  HorizontalList(),
                  SizedBox(width: 10),
                  HorizontalList(),
                  SizedBox(width: 10),
                  HorizontalList(),
                ],
              ),
             )
            ],
          ),

暫無
暫無

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

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