Flutter- Stack尽管存在子元素,但仍然占用很多高度。

huangapple go评论73阅读模式
英文:

Flutter- Stack takes a lot of height despite the children

问题

I'm using Listview.builder to show products.
I use mediaQuery to check if screen width is big enough then it would use GridView.builder() as shown in the image below. However, there is a problem with GridView.builder() only, which is that it exceeds the Stack Height. I checked with Flutter DevTools and found that the column inside the stack is just taking its minimum size. So, what causes the stack to be this big?

child: Stack(
  children: [
    Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        // Work Image
        AspectRatio(
          aspectRatio: 16.0 / 9.0,
          child: Image.asset(
            Constants.appLogo,
            fit: BoxFit.cover,
          ),
        ),
        Padding(
          padding: const EdgeInsets.only(
            left: Sizes.PADDING_18,
            bottom: Sizes.PADDING_8,
            right: Sizes.PADDING_8,
          ),
          // Work Title, Address
          child: Row(
            children: [
              Flexible(
                child: Text(
                  testAddress.length > 25
                      ? '${testAddress.substring(0, 25)}...'
                      : testAddress,
                  overflow: TextOverflow.ellipsis,
                  maxLines: 1,
                  style: const TextStyle(
                    fontWeight: FontWeight.w600,
                    fontSize: Sizes.TEXT_SIZE_22,
                  ),
                ),
              ),
            ],
          ),
        ),
        Padding(
          padding: const EdgeInsets.only(
            left: Sizes.PADDING_18,
            bottom: Sizes.PADDING_8,
            right: Sizes.PADDING_8,
          ),
          // Subtitle
          child: Row(
            children: [
              Flexible(
                child: FittedBox(
                  fit: BoxFit.scaleDown,
                  alignment: Alignment.bottomLeft,
                  child: Text(
                    testAddress.length > 34
                        ? '${testAddress.substring(0, 34)}...'
                        : testAddress,
                    overflow: TextOverflow.ellipsis,
                    maxLines: 1,
                    style: TextStyle(
                      fontSize: Sizes.TEXT_SIZE_14,
                      color: Colors.grey.withOpacity(0.8),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ],
    ),
  ],
),

Flutter- Stack尽管存在子元素,但仍然占用很多高度。

英文:

I'm using Listview.builder to show products.
I use mediaQuery to check if screen width is big enough then it would use Grideview.builder() as show in image below but there is a problem with Grideview.builder() only which is this excees Stack Height, i checked with flutter devTools and found the the column inside stack is just taking it min size so what causes stack to be this big?

child: Stack(
    children: [
      Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          // Work Image
          AspectRatio(
            aspectRatio: 16.0 / 9.0,
            child: Image.asset(
              Constants.appLogo,
              fit: BoxFit.cover,
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(
              left: Sizes.PADDING_18,
              bottom: Sizes.PADDING_8,
              right: Sizes.PADDING_8,
            ),
            // Work Title, Adress
            child: Row(
              children: [
                Flexible(
                  child: Text(
                    testAdress.length > 25
                        ? '${testAdress.substring(0, 25)}...'
                        : testAdress,
                    overflow: TextOverflow.ellipsis,
                    maxLines: 1,
                    style: const TextStyle(
                      fontWeight: FontWeight.w600,
                      fontSize: Sizes.TEXT_SIZE_22,
                    ),
                  ),
                ),
              ],
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(
              left: Sizes.PADDING_18,
              bottom: Sizes.PADDING_8,
              right: Sizes.PADDING_8,
            ),
            //SubTitle
            child: Row(
              children: [
                Flexible(
                  child: FittedBox(
                    fit: BoxFit.scaleDown,
                    alignment: Alignment.bottomLeft,
                    child: Text(
                      testAdress.length > 34
                          ? '${testAdress.substring(0, 34)}...'
                          : testAdress,
                      overflow: TextOverflow.ellipsis,
                      maxLines: 1,
                      style: TextStyle(
                        fontSize: Sizes.TEXT_SIZE_14,
                        color: Colors.grey.withOpacity(0.8),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
          ],
        ),
      ),
    ],
  ),

Flutter- Stack尽管存在子元素,但仍然占用很多高度。

答案1

得分: 1

在GridView上,默认的childAspectRatio为1,你可以设置为childAspectRatio: 16.0 / 9.0

   childAspectRatio: 16.0 / 9.0,
英文:

Default childAspectRatio:1 on gridView, you can set childAspectRatio: 16.0 / 9.0

   childAspectRatio: 16.0 / 9.0,

huangapple
  • 本文由 发表于 2023年2月24日 02:29:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548901.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定