可滚动的CustomMultiChildLayout

时间:2019-09-10 10:27:03

标签: flutter dart flutter-layout

我无法使CustomMultiChildLayout滚动。

现在,我的CustomMultiChildLayout只是将孩子放在一列中。将来我会使其更复杂,但是现在我想使其可滚动。我尝试将其放入ListView中,但得到了RenderIndexedSemantics object was given an infinite size during layout.。 我尝试将其与SingleChildScrollView一起放在IntrinsicHeight内,但在这种情况下,它无法滚动,并且其高度与视口相同。

以下是一些无法正常运行的示例:

return SingleChildScrollView(
        child: IntrinsicHeight(
          child: CustomMultiChildLayout(
            delegate: ViewLayoutBuilder(itemCount: cards.length),
            children: _buildChildren(context),
          ),
        ),
      );

return ListView(
        children: <Widget>[
          CustomMultiChildLayout(
            delegate: ViewLayoutBuilder(itemCount: cards.length),
            children: _buildChildren(context),
          )
        ],
      )

这是我的ViewLayoutBuilder代表:

class ViewLayoutBuilder extends MultiChildLayoutDelegate {
  final int itemCount;

  ViewLayoutBuilder({@required this.itemCount});

  @override
  void performLayout(Size size) {
    Offset position = Offset(0,0);
    Size firstSize = layoutChild('card_0', BoxConstraints.tightFor(width: size.width));
    positionChild('card_0', position);
    position = position.translate(0, firstSize.height);
    for (int i = 1; i < itemCount; i++) {
      final String cardId = 'card_$i';

      if (hasChild(cardId)) {
        Size newSize = layoutChild('$cardId', BoxConstraints.tightFor(width: size.width));
        positionChild('$cardId', position);
        position = position.translate(0, newSize.height);
      }
    }
  }

  @override
  bool shouldRelayout(MultiChildLayoutDelegate oldDelegate) => false;
}

我所有尝试的主要错误是我的CustomMultiChildLayout被赋予了无限高。在其他情况下,它会裁剪到视口高度,无法滚动。

0 个答案:

没有答案