使Column的子(例如容器布局)wrap_content水平

问题描述 投票:1回答:1

如图所示:

两个文本(神奇宝贝,上午11点25分)之间有一条实线。我想要的是线的长度等于最长的文本长度。但该行的行为类似于match_parent。

在Android Native中,我们可以使用垂直LinearLayout,并在水平设置android:layout_width =“wrap_content”限制。

在Flutter中,我们只能在垂直方向设置Column mainAxisSize:MainAxisSize.min限制。

enter image description here

我猜这个问题是由于Divider造成的。当Divider消失时,Column的宽度为wrap_content。

实验如下:

enter image description here

enter image description here

      Container(
        color: Colors.red,
        margin: EdgeInsets.symmetric(horizontal: 8.0),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Container(
              margin: EdgeInsets.symmetric(vertical: 8.0),
              padding: EdgeInsets.all(4.0),
              child: Text(
                'Pokémon',
                style: TextStyle(
                    color: Colors.white,
                    fontSize: 18.0,
                    fontWeight: FontWeight.bold),
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
              ),
            ),
            Divider(
              height: 2.0,
              color: Colors.white,
            ),
            Container(
              margin: EdgeInsets.only(top: 8.0),
              child: Text(
                '11.25 AM',
                style: TextStyle(
                    color: Colors.white,
                    fontSize: 12.0,
                    fontWeight: FontWeight.bold),
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
              ),
            ),
          ],
        ),
      ),
flutter
1个回答
1
投票

你可以使用 - IntrinsicWidth小部件。用它包裹柱子。

Container(
          color: Colors.red,
          margin: EdgeInsets.symmetric(horizontal: 8.0),
          child: IntrinsicWidth(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Container(
                  margin: EdgeInsets.symmetric(vertical: 8.0),
                  padding: EdgeInsets.all(4.0),
                  child: Text(
                    'Pokémon',
                    style: TextStyle(
                        color: Colors.white,
                        fontSize: 18.0,
                        fontWeight: FontWeight.bold),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
                Divider(
                  height: 2.0,
                  color: Colors.white,
                ),
                Container(
                  margin: EdgeInsets.only(top: 8.0),
                  child: Text(
                    '11.25 AM',
                    style: TextStyle(
                        color: Colors.white,
                        fontSize: 12.0,
                        fontWeight: FontWeight.bold),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
              ],
            ),
          ),
        ),

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.