群里經(jīng)常有小伙伴問(wèn),如何讓text不溢出?。窟@個(gè)問(wèn)題并不難啊,似乎小伙伴們不知道如何去控制。
今天就以row組件為例,比如row組件里放了一個(gè)text和一個(gè)icon,現(xiàn)在這個(gè)row是占了手機(jī)的一行,如果text文本過(guò)長(zhǎng),很明顯會(huì)造成溢出異常的
第一個(gè)技巧
overflow: TextOverflow.ellipsis
第二個(gè)技巧
constraints: BoxConstraints(
maxWidth: MediaQuery.of(context).size.width - 50),
一般要搭配使用
Widget renderAddress() {
if (address != null) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
children: <Widget>[
Container(
constraints: BoxConstraints(
maxWidth: MediaQuery.of(context).size.width - 50),
child: new Text(
address.areaName + address.housePlate,
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w500,
fontSize: 20),
),
),
Icon(
Icons.chevron_right,
color: Colors.white70,
)
],
),
Container(
height: 4,
),
Text(
"${address.customerName} ${address.phone}",
style: TextStyle(
color: Colors.white,
fontSize: 17,
fontWeight: FontWeight.w500,
letterSpacing: 1),
),
]);
}
return Row(
children: <Widget>[
new Text(
"選擇",
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.w500, fontSize: 21),
),
Icon(
Icons.chevron_right,
color: Colors.white70,
)
],
);
}