【问题标题】:flutter reduce the height of container颤振降低容器的高度
【发布时间】:2021-07-27 02:14:37
【问题描述】:

我将一些 json 数据传递给另一个显示为卡片的小部件。问题是容器会自动占用很大的高度。

这是页面当前的样子。我提供的黑色边框是所需的高度。

这就是我传递数据的方式:

      GridView.count(
        crossAxisCount: 1,
        physics: const ClampingScrollPhysics(),
        shrinkWrap: true,
        children: List.generate(
          widget.recipe!.recipeSteps!.length,
          (index) {
            return Container(
              constraints: BoxConstraints.tightForFinite(height: 10),
              child: StepsCard(
                recipeStep: widget.recipe!.recipeSteps![index].name,
              ),
            );
          },
        ),
      ),

这是StepsCard()的代码:

class StepsCard extends StatelessWidget {
  final String? recipeStep;

  StepsCard({required this.recipeStep});
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(top: 5, bottom: 5),
      child: Container(
          height: 20,
          decoration: BoxDecoration(
            // color: Color(0xfff8f8fa),
            color: Colors.red,
            borderRadius: BorderRadius.circular(15),
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              Text("image"),
              Text(this.recipeStep!),
            ],
          )),
    );
  }
}

如何创建我标记为黑色的大小的容器?

更新 1:我尝试为两个容器提供高度(高度:10),但这在任何级别都不起作用

更新 2:

这是我根据@7mada 给出的建议尝试过的代码

      Container(
        height: 100,
        child: ListView.builder(
          itemCount: widget.recipe!.recipeSteps!.length,
          itemBuilder: (BuildContext context, int index) {
            return StepsCard(
                recipeStep: widget.recipe!.recipeSteps![index].name);
          },
        ),
      ),

它给了我一个高度为 100 的容器,并使里面的内容可滚动,这是有问题的,因为 StepsCard() 位于列的底部,我不想要高度限制。

更新 3:

  Widget _buildItems(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(10),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          SizedBox(
            height: 40,
          ),
          appBar(context),
          SizedBox(
            height: 20,
          ),
          VideoCard(
            url: widget.recipe!.recipeVideoLink,
          ),
          RecipeIntroCard(
            recipeName: widget.recipe!.recipeName,
            category: widget.recipe!.category,
            time: widget.recipe!.time,
            recipeAuthor: widget.recipe!.recipeAuthor,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Text(
                "Ingredients",
                style: TextStyle(
                  color: Color(0xff18172b),
                  fontSize: 25,
                  fontFamily: "Poppins",
                  fontWeight: FontWeight.w400,
                ),
              ),
            ],
          ),
          SizedBox(
            height: 20,
          ),
          Container(
            height: 80.0,
            child: ListView.builder(
              scrollDirection: Axis.horizontal,
              itemCount: widget.recipe!.ingredients!.length,
              itemBuilder: (BuildContext context, int index) {
                return IngredientCard(
                  name: widget.recipe!.ingredients![index].name,
                  amount: widget.recipe!.ingredients![index].amount,
                );
              },
            ),
          ),
          Container(
            padding: EdgeInsets.only(top: 15),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Text(
                  "Steps",
                  style: TextStyle(
                    color: Color(0xff18172b),
                    fontSize: 25,
                    fontFamily: "Poppins",
                    fontWeight: FontWeight.w400,
                  ),
                ),
              ],
            ),
          ),
          Expanded(
            child: ListView.builder(
              itemCount: widget.recipe!.recipeSteps!.length,
              itemBuilder: (BuildContext context, int index) {
                return StepsCard(
                    recipeStep: widget.recipe!.recipeSteps![index].name);
              },
            ),
          ),
        ],
      ),
    );
  }

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您尚未将高度传递给外部容器。你可以这样做

      GridView.count(
        crossAxisCount: 1,
        physics: const ClampingScrollPhysics(),
        shrinkWrap: true,
        children: List.generate(
          widget.recipe!.recipeSteps!.length,
          (index) {
            return Container(
              height: 30.0,
              constraints: BoxConstraints.tightForFinite(height: 10),
              child: StepsCard(
                recipeStep: widget.recipe!.recipeSteps![index].name,
              ),
            );
          },
        ),
      ),
    

    【讨论】:

    • 好吧,我确实试过了,但并没有降低高度
    【解决方案2】:

    更改GridView 中的childAspectRatio 值以获得所需的高度,但是我建议您使用ListView.builderGridView.builder 这样您就可以拥有itemExtent,让您在拥有更好的性能。

    【讨论】:

    • 嗨,我试过ListView.builder。在我用容器包裹它并给出高度之前,它不起作用。现在提供一个高度使容器可滚动,但由于StepsCard() 位于Column 的底部,我想在没有容器高度限制的情况下加载尽可能多的数据
    • Expanded 包裹而不是容器
    • RenderBox was not laid out: RenderPadding#fa470 relayoutBoundary=up12 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE 'package:flutter/src/rendering/box.dart': Failed assertion: line 1930 pos 12: 'hasSize' 这就是我得到的
    • 嗨,我已经分享了全屏代码,您可以检查并告诉它有什么问题吗? @7马达
    • 嗨,childAspectRatio 工作。由于某种原因,它在我重新启动整个系统后起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-25
    • 2021-10-29
    • 1970-01-01
    • 2020-11-19
    • 1970-01-01
    • 2020-10-03
    • 2021-05-15
    相关资源
    最近更新 更多