Flutter - 包裹行展开的小部件

Joz*_*ott 5 dart flutter flutter-layout

我有一个Row孩子们是Expanded小部件的地方。我的目标是如果没有足够的空间容纳所有孩子,则将其包装起来Row(就像 一样)。Wrap

我已经尝试替换RowWrap

return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Wrap(
          children: [
            Expanded(
              child: element("Sun Glasses"),
            ),
          ],
        )
      ),
    );
Run Code Online (Sandbox Code Playgroud)

但这会导致错误:

The following assertion was thrown while applying parent data.:
Incorrect use of ParentDataWidget.

The ParentDataWidget Expanded(flex: 1) wants to apply ParentData of type FlexParentData to a RenderObject, which has been set up to accept ParentData of incompatible type WrapParentData.

Usually, this means that the Expanded widget has the wrong ancestor RenderObjectWidget. Typically, Expanded widgets are placed directly inside Flex widgets.
The offending Expanded is currently placed inside a Wrap widget.
Run Code Online (Sandbox Code Playgroud)

由于行的元素填充它很重要,因此我不能简单地删除 Expanded。有办法实现这一点吗?

这是我正在尝试实现的模型:

在此输入图像描述

Joz*_*ott 13

由于我找不到解决此问题的任何现有解决方案,因此我编写了一个自定义 RenderBox 小部件。

您可以在Github上找到源代码,在pub.dev上找到 flutter 包。

FlexList 的简单示例

SizedBox(
    width: 300,
    child: FlexList(
      horizontalSpacing: 5,
      verticalSpacing: 10,
      children: [
        for (var i = 0; i < 10; i++)
          Container(
            color: Theme
                .of(context)
                .backgroundColor,
            padding: EdgeInsets.symmetric(
                horizontal: 20 + 20 * (i % 4), vertical: 10),
            child: Text("Item $i"),
          )
      ],
    )),
Run Code Online (Sandbox Code Playgroud)

示例渲染