flutter 将行中的所有子级设置为相等大小

rsl1atfo  于 2023-05-29  发布在  Flutter
关注(0)|答案(5)|浏览(169)

我有一个行,有两个这样的孩子:

----------------------
| wide child1 | child2 |
 ----------------------

有没有办法使每个单元格的大小相等,使每个单元格的宽度等于最宽单元格的宽度?像这样:

--------------------------
| wide child1 |   child2   |
 --------------------------

因此,整个Row的宽度为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,
          ),
        ),
      ),
    );
  }
c8ib6hqw

c8ib6hqw1#

将child1和child2 Package 在Expanded中,

Row(
        children: <Widget>[
          Expanded(
            child: Container(
              color: Colors.amber,
              height: 100,
            ),
          ),
          Expanded(
            child: Container(
              color: Colors.amber,
              height: 100,
            ),
          ),
xwbd5t1u

xwbd5t1u2#

我想你想要的是一个Table?例如:

Table(
  columnWidths: const {
    0: FlexColumnWidth(),
    1: FlexColumnWidth(),
  },
  children: [
    TableRow(
      children: [
        Text(
          "Review",
        ),
        Text(
          "Buy",
        ),
      ]
    ),
  ],
)
ukdjmx9f

ukdjmx9f3#

你提到你所有的孩子都是Text小部件。我们可以渲染文本以了解其大小(reference),选择最大宽度并使用MultiChildLayoutDelegate进行布局。这是一个有点hacky,但将工作:

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;
}
rdrgkggo

rdrgkggo4#

使用具有相同 flex 值的Expanded可沿着主轴平分空间。在这里你必须了解主轴的要点是什么。我建议仔细阅读这个链接:https://api.flutter.dev/flutter/widgets/Column-class.html
对于“列”小部件,主轴是垂直y轴,而“行”小部件具有水平x轴作为其主轴。您可以检查下面的代码,以了解如何使用扩展。
使用DartPad(https://dartpad.dev/?id)尝试更多示例,了解Expanded的工作原理。

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    
    return Column(
      children: <Widget>[
        Expanded(
          flex: 1,
          child: Container(
            color: Colors.amber,
          ),
        ),
        Expanded(
          flex: 1,
          child: Container(
            color: Colors.red,
          ),
        ),
        Expanded(
          flex: 1,
          child: Container(
            color: Colors.green,
          ),
        ),
        Expanded(
          flex: 1,
          child: Container(
            color: Colors.blue,
          ),
        ),
      ],
    );
  
  }
}

输出:

将上面的“列”替换为“行”,您将得到以下输出:

eivgtgni

eivgtgni5#

您可以使用相同的flex编号将Flexible中的每个小部件 Package 起来

相关问题