flutter - 使 Column 的子项(例如容器布局)水平 wrap_content
问题描述
如图所示:
两个文本之间有一条实线(神奇宝贝,上午 11.25)。我想要的是等于最长文本长度的行的长度。但是该行的行为类似于 match_parent。
在 Android Native 中,我们可以使用垂直 LinearLayout,并在水平方向设置 android:layout_width="wrap_content" 限制。
在 Flutter 中,我们只能在垂直方向设置 Column mainAxisSize: MainAxisSize.min 限制。
我猜这个问题是由于分频器。当 Divider 消失时,Column 的宽度是 wrap_content。
实验如下:
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,
),
),
],
),
),
解决方案
您可以使用 -IntrinsicWidth
小部件。用它包裹 Column。
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,
),
),
],
),
),
),
推荐阅读
- python - Django DRY 模型属性、类和触发器
- java - 为什么 Java 中浮点数(32 位)的指数是 -126 而不是 -128?
- android - php my admin my sql database 和 android studio 无法连接到本地主机
- powershell - 无法使用powershell生成的随机密码打开7z文件
- mysql - 基于角色的访问控制 MySql 实现
- git - 在 Windows 上使用 Git Bash,如何签出以 Unicode 字符开头的分支?
- php - 在 Laravel 中插入数据的最快方法
- spring - 无法比较字符串
- javascript - JS - 从同一类中的其他方法调用类中的方法
- sequelize.js - 按第一个模型排序