拉伸行小部件时出现BoxConstraints错误

时间:2020-08-02 05:07:19

标签: flutter

我在一个Card小部件中有一个ListView小部件,该卡具有以下代码结构:

return Card(
      child: Row(
        // crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          Image(
            image: AssetImage('assets/images/my-img.jpg'),
            width: 150,
          ),
          Expanded(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween, // THIS HAS NO EFFECT
              children: <Widget>[
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(content.title),
                    Text(content.description),
                  ],
                ),
                Row(
                  // crossAxisAlignment: CrossAxisAlignment.baseline,
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Row(
                      children: <Widget>[
                        Icon(Icons.accessibility),
                        Text('cool')
                      ],
                    ),
                    Text('something else'),
                  ],
                )
              ],
            ),
          ),
        ],
      ),
    );

我正在尝试使column中的row具有spaceBetween对齐方式,因为现在column垂直居中。我尝试将CrossAxisAlignment.stretch添加到Row小部件中,但出现错误:

BoxConstraints强制无限高

2 个答案:

答案 0 :(得分:0)

Column占据所有可用高度。考虑用固定高度ColumnExpanded包裹在Container内。

要使Column占用尽可能多的空间,请设置mainAxisSize: MainAxisSize.min。但是,所有小部件都已打包,所有Alignments均无效。

因此,您可能正在寻找的是将height的{​​{1}}设置为其Row children的最大高度。这可以通过用widgetRow包裹起来来实现。

IntrinsicHeight

答案 1 :(得分:0)

您可以在列和行之间添加一个SizedBox。

Expanded(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(content.title),
                    Text(content.description),
                  ],
                ),
                //Sized Box
                SizedBox(height: //whatever double),
                Row(
                  crossAxisAlignment: CrossAxisAlignment.baseline,
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Row(
                      children: <Widget>[
                        Icon(Icons.accessibility),
                        Text('cool')
                      ],
                    ),
                    Text('something else'),
                  ],
                )