Text構造函數的源碼以下面代碼所示html
const Text(this.data, {
Key key,
this.style,
this.strutStyle,
this.textAlign,
this.textDirection,
this.locale,
this.softWrap,
this.overflow,
this.textScaleFactor,
this.maxLines,
this.semanticsLabel,
}) : assert(
data != null,
'A non-null String must be provided to a Text widget.',
),
textSpan = null,
super(key: key);
複製代碼
須要顯示的字符串git
Text(
'Hello World',
)
複製代碼
運行結果以下圖所示 github
文本樣式,樣式屬性以下表所示redux
Text(
'Hello World',
style: TextStyle(
color: Colors.green,
fontSize: 16.0,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.italic,
letterSpacing: 2.0,
wordSpacing: 1.5,
textBaseline: TextBaseline.alphabetic,
decoration: TextDecoration.lineThrough,
decorationStyle: TextDecorationStyle.dashed,
decorationColor: Colors.blue
),
)
複製代碼
運行結果以下圖所示 數組
文本對齊方式,參數以下面表格所示ide
Column(
children: <Widget>[
Text(
text,
textAlign: TextAlign.left,
style: TextStyle(
color: Colors.green,
),
),
Text(
text,
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.black,
),
),
Text(
text,
textAlign: TextAlign.right,
style: TextStyle(
color: Colors.yellow,
),
),
Text(
text,
textAlign: TextAlign.start,
style: TextStyle(
color: Colors.red,
),
),
Text(
text,
textAlign: TextAlign.end,
style: TextStyle(
color: Colors.orange,
),
),
Text(
text,
textAlign: TextAlign.justify,
style: TextStyle(
color: Colors.blue,
),
),
],
)
複製代碼
運行結果以下圖所示 函數
TextDirection.ltr
,文本從左向右流動;TextDirection.rtl
,文本從右向左流動。 相對TextAlign中的start、end而言有用(當start使用了ltr至關於end使用了rtl,也至關於TextAlign使用了left)post
Column(
children: <Widget>[
Text(
text,
textDirection: TextDirection.ltr,
style: TextStyle(
color: Colors.green,
),
),
Text(
text,
textDirection: TextDirection.rtl,
style: TextStyle(
color: Colors.blue,
),
),
],
)
複製代碼
運行結果以下圖所示 學習
此屬性不多設置,用於選擇區域特定字形的語言環境字體
是否自動換行,若爲false,文字將不考慮容器大小,單行顯示,超出屏幕部分將默認截斷處理
Column(
children: <Widget>[
Text(
text,
softWrap: false,
style: TextStyle(
color: Colors.green,
),
),
Text(
text,
softWrap: true,
style: TextStyle(
color: Colors.blue,
),
),
],
)
複製代碼
運行結果以下圖所示
處理溢出文本,參數以下面表格所示
Column(
children: <Widget>[
Text(
text,
overflow: TextOverflow.clip,
maxLines: 3,
style: TextStyle(
color: Colors.green,
),
),
Text(
text,
overflow: TextOverflow.fade,
maxLines: 3,
style: TextStyle(
color: Colors.blue,
),
),
Text(
text,
overflow: TextOverflow.ellipsis,
maxLines: 3,
style: TextStyle(
color: Colors.yellow,
),
),
],
)
複製代碼
運行結果以下圖所示
字體顯示倍率
Column(
children: <Widget>[
Text(
text,
style: TextStyle(
color: Colors.green,
),
),
Text(
text,
textScaleFactor: 2,
style: TextStyle(
color: Colors.blue,
),
),
],
)
複製代碼
運行結果以下圖所示
最大行數
Column(
children: <Widget>[
Text(
text,
style: TextStyle(
color: Colors.green,
),
),
Text(
text,
maxLines: 3,
style: TextStyle(
color: Colors.blue,
),
),
],
)
複製代碼
運行結果以下圖所示
在上面的例子中,Text的全部文本內容只能按同一種樣式進行展現,若是咱們須要對一個Text內容的不一樣部分按照不一樣的樣式顯示,那又該怎麼處理?此時須要用到Text.rich
。
const Text.rich(this.textSpan, {
Key key,
this.style,
this.strutStyle,
this.textAlign,
this.textDirection,
this.locale,
this.softWrap,
this.overflow,
this.textScaleFactor,
this.maxLines,
this.semanticsLabel,
}) : assert(
textSpan != null,
'A non-null TextSpan must be provided to a Text.rich widget.',
),
data = null,
super(key: key);
複製代碼
Text.rich
中除了textSpan
屬性跟Text
不同,其餘都同樣。textSpan
屬性以下所示
TextSpan
定義以下
const TextSpan({
this.style,
this.text,
this.children,
this.recognizer,
});
複製代碼
其中style
和text
屬性表明該文本的樣式和內容;children
是一個TextSpan的數組,也就是說TextSpan
能夠包括其餘TextSpan
;而recognizer
用於對該文本片斷上用於手勢進行識別處理。下面看一個例子
Text.rich(TextSpan(
children: [
TextSpan(
text: "百度: "
),
TextSpan(
text: "https://www.baidu.com",
style: TextStyle(
color: Colors.blue
),
),
]
))
複製代碼
運行結果以下圖所示
在widget樹中,文本的樣式默認是能夠被繼承的,父節點的文本樣式子節點默認會繼承,若是子節點中從新設置了默認樣式的某些屬性,那麼則以子節點設置的爲準。咱們也能夠經過設置inherit: false
不繼承父節點的默認樣式。下面咱們看一個例子
DefaultTextStyle(
textAlign: TextAlign.left,
style: TextStyle(
fontSize: 20.0,
color: Colors.green,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.italic),
child: Column(
children: <Widget>[
Text(
text,
),
Text(
text,
style: TextStyle(
color: Colors.yellow,
),
),
Text(
text,
style: TextStyle(
inherit: false,
color: Colors.blue,
),
),
],
))
複製代碼
運行結果以下圖所示
好了,Text
相關的內容大概就這麼多。更詳細的請看官方文檔