Flutter(十)基礎(chǔ)控件-Text

感君一回顧,思君朝與暮。

<一>Text類似iOS中的UILabel控件

Text 屬性 介紹
style TextStyle 對(duì)象,最常用屬性,詳情見(jiàn)下方表格
strutStyle 字體在文本內(nèi)的一些偏移,使用時(shí) style 屬性必須有值,很少使用
textAlign 對(duì)齊方式:left、start、right、end、center、justify
textDirection TextDirection.ltr:從左到右、TextDirection.rtl:從右到左
locale 區(qū)域設(shè)置,基本不會(huì)用
softWrap 是否自動(dòng)換行
overflow 超出部分截取方式,clip->直接截取,fade->漸隱,ellipsis->省略號(hào)
textScaleFactor 字體縮放
maxLines 最多顯示行數(shù)
semanticsLabel 語(yǔ)義標(biāo)簽,如文本為"$$",這里設(shè)置為"雙美元"
textWidthBasis 測(cè)量行寬度
textHeightBehavior 官方備注: 定義如何應(yīng)用第一行的ascent和最后一行的descent

TextStyle屬性

TextStyle 屬性 介紹
inherit 是否繼承父類
color 字體顏色
backgroundColor 背景色
fontSize 字體大小
fontWeight 字體加粗
fontStyle 系統(tǒng)字體
fontFamily 字體
letterSpacing 字母間距
wordSpacing 單詞間距
textBaseline 字體基線,有興趣的可以單獨(dú)了解,flex 布局中會(huì)有一種baseline,不常用
height 高度
locale 區(qū)域設(shè)置
foreground 前置層
background 背景層
shadows 陰影
fontFeatures 指定字體的多種變體
decoration 文字劃線:上,中,下
decorationColor 劃線顏色
decorationStyle 劃線樣式:虛線,單線,雙線
decorationThickness 線寬,默認(rèn)1.0
debugLabel 僅在 debug 模式下有用

<二>#代碼

  • 正常文本
    Text _normalText(){
    return const Text(
      "Hello World",
      textDirection: TextDirection.ltr,
      textAlign: TextAlign.center,
      softWrap: true,
      maxLines: 2,
      overflow: TextOverflow.ellipsis,
      style: TextStyle(
        fontSize: 50,
        color: Colors.red,
        decoration: TextDecoration.underline,
        decorationColor: Colors.yellow,
        decorationStyle: TextDecorationStyle.double,
      ),
    );
    }
    
  • 富文本
    Text _richText(){
      return Text.rich(
        TextSpan(
          text: "Just ",
          style: const TextStyle(
            fontSize: 30,
          ),
          children: [
            TextSpan(
              text: "taps here",
              style: TextStyle(
                  fontSize: 40,
                  color: Colors.blue
              ),
              recognizer: TapGestureRecognizer()
                ..onTap = (){
                  print("點(diǎn)擊了");
                }
              ,
            ),
            const TextSpan(
              text: " one more time",
            ),
          ],
        ),
      );
    }
    
最后編輯于
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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