如何在 CustomMultiChildLayout 中拥有动态高度?
How to have dynamic height in CustomMultiChildLayout?
我创建了一个 CustomMultiChildLayout
以自定义其 children 的位置。然后尝试将 CustomMultiChildLayout
放入 Container
和 BoxConstraints(maxHeight: 500)
,然后 CustomMultiChildLayout
始终将 maxHeight 500 作为高度。它不能根据它的 children.
调整它的高度
那么如何为我的 CustomMultiChildLayout
设置动态高度?如果这不可能,还有其他方法可以实现这种布局吗?
背景:
我定义了3个widget,每个都是一个Container
,我需要自定义它们的位置
SellerWidget 应位于其他两个小部件的中间。
请参考这张图:
这是我的代码:
class ProductComboWidget extends StatelessWidget {
final Product _product;
ProductComboWidget(this._product);
@override
Widget build(BuildContext context) {
return Container(
constraints: BoxConstraints(maxHeight: 500),
child: CustomMultiChildLayout(
delegate: _TitleInfoLayout(),
children: <Widget>[
LayoutId(
id: _TitleInfoLayout.title,
child: ProductTitleWidget(_product)
),
LayoutId(
id: _TitleInfoLayout.info,
child: ProductInfoWidget(_product)
),
LayoutId(
id: _TitleInfoLayout.seller,
child: SellerWidget(_product.seller)
),
],
),
);
}
}
class _TitleInfoLayout extends MultiChildLayoutDelegate {
static const String title = 'title';
static const String info = 'info';
static const String seller = 'seller';
@override
void performLayout(Size size) {
final Size titleSize = layoutChild(title, BoxConstraints(maxWidth: size.width));
positionChild(title, Offset(0, 0));
layoutChild(info, BoxConstraints(maxWidth: size.width));
positionChild(info, Offset(0, titleSize.height));
final Size sellerSize = layoutChild(seller, BoxConstraints());
positionChild(seller, Offset(size.width - sellerSize.width - 20, titleSize.height - sellerSize.height / 2));
}
@override
bool shouldRelayout(MultiChildLayoutDelegate oldDelegate) {
return true;
}
}
实际结果:
CustomMultiChildLayout
的高度总是等于BoxConstraint.maxHeight,无法根据children调整高度。
请参考这张图:
这个小部件的底部有一个无用的空白space。
预期结果:
像其他小部件一样,使其高度适应 children。
您可以使用 SingleChildScrollView
作为父控件,以根据子控件的数量获取父控件的动态高度。
受 Column
和 Stack
实施的启发,我创建了一个自定义布局来完成这项工作。 https://gist.github.com/jxw1102/a9b58a78a80c8e2f54233b418429fa50
我创建了一个 CustomMultiChildLayout
以自定义其 children 的位置。然后尝试将 CustomMultiChildLayout
放入 Container
和 BoxConstraints(maxHeight: 500)
,然后 CustomMultiChildLayout
始终将 maxHeight 500 作为高度。它不能根据它的 children.
那么如何为我的 CustomMultiChildLayout
设置动态高度?如果这不可能,还有其他方法可以实现这种布局吗?
背景:
我定义了3个widget,每个都是一个Container
,我需要自定义它们的位置
SellerWidget 应位于其他两个小部件的中间。
请参考这张图:
这是我的代码:
class ProductComboWidget extends StatelessWidget {
final Product _product;
ProductComboWidget(this._product);
@override
Widget build(BuildContext context) {
return Container(
constraints: BoxConstraints(maxHeight: 500),
child: CustomMultiChildLayout(
delegate: _TitleInfoLayout(),
children: <Widget>[
LayoutId(
id: _TitleInfoLayout.title,
child: ProductTitleWidget(_product)
),
LayoutId(
id: _TitleInfoLayout.info,
child: ProductInfoWidget(_product)
),
LayoutId(
id: _TitleInfoLayout.seller,
child: SellerWidget(_product.seller)
),
],
),
);
}
}
class _TitleInfoLayout extends MultiChildLayoutDelegate {
static const String title = 'title';
static const String info = 'info';
static const String seller = 'seller';
@override
void performLayout(Size size) {
final Size titleSize = layoutChild(title, BoxConstraints(maxWidth: size.width));
positionChild(title, Offset(0, 0));
layoutChild(info, BoxConstraints(maxWidth: size.width));
positionChild(info, Offset(0, titleSize.height));
final Size sellerSize = layoutChild(seller, BoxConstraints());
positionChild(seller, Offset(size.width - sellerSize.width - 20, titleSize.height - sellerSize.height / 2));
}
@override
bool shouldRelayout(MultiChildLayoutDelegate oldDelegate) {
return true;
}
}
实际结果:
CustomMultiChildLayout
的高度总是等于BoxConstraint.maxHeight,无法根据children调整高度。
请参考这张图:
这个小部件的底部有一个无用的空白space。
预期结果:
像其他小部件一样,使其高度适应 children。
您可以使用 SingleChildScrollView
作为父控件,以根据子控件的数量获取父控件的动态高度。
受 Column
和 Stack
实施的启发,我创建了一个自定义布局来完成这项工作。 https://gist.github.com/jxw1102/a9b58a78a80c8e2f54233b418429fa50