我的行中有2个这样的孩子:
----------------------
| wide child1 | child2 |
----------------------
是否有任何方法可以使每个像元大小相等,以使每个像元的宽度等于最宽像元的宽度?像这样:
--------------------------
| wide child1 | child2 |
--------------------------
因此整个行的宽度为biggestCellWidth * numOfChildren
。
我无法通过内置的小部件实现这种行为,并尝试实现MultiChildLayoutDelegate
,但是由于我无法测量孩子,因此它也无法正常工作。
更新:
// in build function
Container(
height: 48,
child: Material(
clipBehavior: Clip.antiAlias,
borderRadius: BorderRadius.circular(16),
color: Theme.of(context).colorScheme.primary,
child: Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
// this widgets should be equal in width
_buildButton(
text: "Review",
onTap: _onReviewTap,
),
_buildButton(
text: "Buy",
onTap: _onBuyTap,
),
],
),
),
);
Widget _buildButton({
@required String text,
@required Function onTap,
@required EdgeInsets padding,
}) {
return InkWell(
onTap: onTap,
child: Center(
child: Text(
text,
style: TextStyle(
color: Theme.of(context).colorScheme.onPrimary,
),
),
),
);
}
答案 0 :(得分:2)
我认为您想要的是Table?例如:
Table(
columnWidths: const {
0: FlexColumnWidth(),
1: FlexColumnWidth(),
},
children: [
TableRow(
children: [
Text(
"Review",
),
Text(
"Buy",
),
]
),
],
)
答案 1 :(得分:1)
您提到您所有的孩子都是Text
小部件。我们可以渲染文本以了解其大小(reference),选择最大宽度并使用MultiChildLayoutDelegate
进行布局。有点黑,但可以使用:
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
final texts = [
Text('loooooooooooong text'),
Text('short one'),
Text('one more'),
];
final children = <Widget>[
for (int i = 0; i < texts.length; i++)
LayoutId(
id: '$_kLayoutKey$i',
child: Container(
color: Color.fromARGB(255, Random().nextInt(255), Random().nextInt(255), Random().nextInt(255)),
child: texts[i],
),
),
];
return Scaffold(
appBar: AppBar(),
body: SafeArea(
child: CustomMultiChildLayout(
delegate: _CircularLayoutDelegate(texts, 14),
children: children,
)
),
);
}
}
const String _kLayoutKey = 'test';
class _CircularLayoutDelegate extends MultiChildLayoutDelegate {
_CircularLayoutDelegate(this.texts, this.fontSize);
final double fontSize;
final List<Text> texts;
double _calcTextWidth(BoxConstraints constraints, Text textWidget) {
RenderParagraph renderParagraph = RenderParagraph(
TextSpan(
text: textWidget.data,
style: TextStyle(
fontSize: fontSize,
),
),
textDirection: TextDirection.ltr,
maxLines: 1,
);
renderParagraph.layout(constraints);
return renderParagraph.getMinIntrinsicWidth(fontSize).ceilToDouble();
}
@override
void performLayout(Size size) {
final textSizes = [
for (final text in texts)
_calcTextWidth(BoxConstraints.loose(size), text),
];
final maxWidth = textSizes.fold<double>(0, (p, v) {
final textWidth = v;
return textWidth > p ? textWidth : p;
});
final textConstraint = BoxConstraints(
maxWidth: maxWidth,
minWidth: maxWidth,
maxHeight: size.height,
);
for (int i = 0; i < texts.length; i++) {
final String childId = '$_kLayoutKey$i';
if (hasChild(childId)) {
layoutChild('$_kLayoutKey$i', textConstraint);
positionChild(
childId,
Offset(maxWidth * i, 0),
);
}}
}
@override
bool shouldRelayout(_CircularLayoutDelegate oldDelegate) => oldDelegate.texts != texts;
}
答案 2 :(得分:-1)
将您的child1和child2包裹在Expanded内,
Row(
children: <Widget>[
Expanded(
child: Container(
color: Colors.amber,
height: 100,
),
),
Expanded(
child: Container(
color: Colors.amber,
height: 100,
),
),
答案 3 :(得分:-2)
您可以使用相同的弹性编号将每个小部件包裹在Flexible中