flutter 如何防止文本的溢出

群里經(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,
      )
    ],
  );
}




?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容