感君一回顧,思君朝與暮。
<一>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", ), ], ), ); }