【问题标题】:Image widget: the height does not match the parent图像小部件:高度与父级不匹配
【发布时间】:2019-12-27 04:15:49
【问题描述】:

我是一名 Android 开发人员,我正在尝试开发一个非常简单的应用程序来发现 Flutter。

我想创建一个包含非常简单单元格的列表。一张卡片:

  • 左侧是固定宽度的图像。高度应与父容器匹配;
  • 右侧是一些垂直堆叠的文本字段。

我可以正确对齐小部件,但图像无法将高度属性设置为“匹配父级”。

这是我当前的树:

Widget _buildTabBarView({@required List<AttractionCategory> categories})
  {
    return TabBarView(
      children: <Widget>[
        for(var category in categories)
          Container(
            child: ListView.builder(
              padding: EdgeInsets.all(8),
              itemCount: category.attractions.length,
              itemBuilder: (context, index)
              {
                return Card(
                  clipBehavior: Clip.antiAliasWithSaveLayer,
                  child: Row(
                    mainAxisSize: MainAxisSize.max,
                    children: <Widget>[
                      Padding(
                        padding: EdgeInsets.only(right: 8),
                        child: Image(
                          fit: BoxFit.fill,
                          width: 125,
                          image: AssetImage(category.attractions[index].photo),
                        ),
                      ),
                      Expanded(
                        child: Column(
                          children: <Widget>[
                            Padding(
                              padding: EdgeInsets.only(bottom: 8, top: 8, right: 8),
                              child: Align(
                                alignment: Alignment.topLeft,
                                child: Text(
                                  category.attractions[index].name,
                                  style: Theme.of(context).textTheme.title,
                                ),
                              ),
                            ),
                            Padding(
                              padding: EdgeInsets.only(right: 8, bottom: 8),
                              child: Align(
                                alignment: Alignment.topLeft,
                                child: Text(
                                  category.attractions[index].description,
                                  style: Theme.of(context).textTheme.body1,
                                ),
                              ),
                            ),
                            (category.attractions[index].size != null) ? Padding(
                              padding: EdgeInsets.only(right: 8, bottom: 8),
                              child: Align(
                                alignment: Alignment.topLeft,
                                child: Row(
                                  children: <Widget>[
                                    Padding(
                                      padding: EdgeInsets.only(right: 8),
                                      child: Icon(
                                        Icons.accessibility_new,
                                        size: 16,
                                      ),
                                    ),
                                    Text(
                                      category.attractions[index].size,
                                      style: Theme.of(context).textTheme.body1.copyWith(color: Color.fromARGB(255, 57, 180, 54)),
                                    ),
                                  ],
                                ),
                              ),
                            ) : Container(),
                          ],
                        ),
                      ),
                    ],
                  ),
                );
              },
            ),
          ),
      ],
    );
  }

这里是输出:

如您所见,Image 的高度与父级不匹配。

我怎样才能做到这一点?

如果我的树很糟糕,请毫不犹豫地向我推荐一棵全新的树!

感谢您的帮助!

【问题讨论】:

    标签: flutter flutter-layout flutter-image


    【解决方案1】:

    用以下内容替换您的图片,包括周围的填充:

                    Container(
                  width: 125.0,
                  decoration: BoxDecoration(
                    image: DecorationImage(
                        image: AssetImage("myphoto"),
                    fit: BoxFit.fill),
                  ),
                ),
    

    请参考这里,只是google上的第一个结果:

    How do I stretch an image to fit the whole background (100% height x 100% width) in Flutter?

    【讨论】:

    • 感谢您的帮助,但它不起作用。如果我不在容器上使用height 属性,则不会显示图像。
    • 你在 ListView() 中使用这个吗?你能发布更多使用它的上下文代码吗?
    • 是的,我在 ListView 中使用它。我已经用构建小部件树的整个函数更新了我的帖子。
    • 问题是,当你的图像的纵横比不能覆盖给定宽度的高度时。在您的示例中,如果您的图像是 3:1 (w,h) ,如果您的卡片高度超过 41,66 (125/3),则图像无法覆盖给定宽度的高度。然后它必须在 y 轴上拉伸,这不是你想要的,对吧?给它一个宽度,比如说 160,它应该可以按您的预期工作。
    • 这是否意味着我们不能在 Android 上应用与缩放类型“中心裁剪”相同的行为? :(
    猜你喜欢
    • 2017-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-23
    • 2021-02-14
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    相关资源
    最近更新 更多