Text(文本組件)負(fù)責(zé)顯示文本和定義顯示樣式力麸,其提供了多個(gè)構(gòu)造函數(shù):
-
new Text
:創(chuàng)建一個(gè)文本組件可款; -
new Text.rich
:創(chuàng)建一個(gè)擁有TextSpan
的文本組件育韩。
Text組件顯示一個(gè)樣式單一的文本字符串。根據(jù)布局約束筑舅,字符串可能跨多行顯示座慰,也可能全部顯示在同一行上。
樣式參數(shù)時(shí)可選的翠拣。當(dāng)省略時(shí)版仔,文本將使用最接近的DefaultTextStyle
中的樣式。如果給定樣式的TextStyle.inherit
屬性為true(默認(rèn))误墓,則給定樣式將于最接近的DefaultTextStyle
的樣式合并蛮粮。這種合并行為是很有用的,例如谜慌,使用默認(rèn)字體系列和大小時(shí)使文本粗體顯示然想。
//Text構(gòu)造函數(shù)
body: Center(
child: Text(
'Hello, $name! How are you?',
textAlign: TextAlign.center,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontWeight: FontWeight.bold),
),
),
//Text.rich構(gòu)造函數(shù)
body: Center(
child: const Text.rich(
TextSpan(
text: 'Hello',
children: <TextSpan>[
TextSpan(text: ' beautiful ', style: TextStyle(fontStyle: FontStyle.italic)),
TextSpan(text: 'world', style: TextStyle(fontWeight: FontWeight.bold))
]
)
)
),
交互:
- 若使用文本對(duì)觸摸事件作出反應(yīng),請(qǐng)將其包裝在
GestureDetector
組件中欣范,并使用GestureDetector.onTap
處理变泄。 - 在
material design
應(yīng)用程序中,考慮使用FlatButton
代替恼琼,或者如果不適用妨蛹,至少使用InkWell
來(lái)代替GestureDetector
。 - 若使用部分文本做交互晴竞,需要使用
RichText
蛙卤,并將TapeReservator
指定為文本相關(guān)部分的TextSpan.recognizer
。-
RichText
:提供了對(duì)文本樣式的更多控制噩死; -
DefaultTextStyle
:為文本組件提供默認(rèn)樣式颤难。
-
繼承:
Object > Diagnosticable > DiagnosticableTree > Widget > StatelessWidget > Text
構(gòu)造函數(shù):
//Text構(gòu)造函數(shù)
const Text(String data, {//要顯示的文本
Key key,
TextStyle style,//文本樣式
StrutStyle strutStyle,//支撐樣式
TextAlign textAlign,//文本水平對(duì)齊方式
TextDirection textDirection,//文本顯示的方向
Locale locale,//用于在相同Unicode字符可以根據(jù)區(qū)域設(shè)置不同時(shí)選擇字體
bool softWrap,//文本是否應(yīng)該在換行符處斷行
TextOverflow overflow,//處理溢出
double textScaleFactor,//每個(gè)邏輯像素的字體像素值
int maxLines,//文本可顯示多少行
String semanticsLabel,//該文本的另一種語(yǔ)義標(biāo)簽
})
//Text.rich構(gòu)造函數(shù)
const Text.rich(TextSpan textSpan, {//以`TextSpan`方式顯示文本
Key key,
TextStyle style,
StrutStyle strutStyle,
TextAlign textAlign,
TextDirection textDirection,
Locale locale,
bool softWrap,
TextOverflow overflow,
double textScaleFactor,
int maxLines,
String semanticsLabel,
})
屬性:
名稱 | 類型 | 說(shuō)明 |
---|---|---|
data |
String |
要顯示的文本 |
locale |
Locale |
用于在相同Unicode字符可以根據(jù)區(qū)域設(shè)置不同時(shí)選擇字體 |
maxLines |
int |
文本可顯示多少行。如果文本超過(guò)給定的行數(shù)已维,則根據(jù)溢出截?cái)唷?/td> |
overflow |
TextOverflow |
處理溢出 |
semanticsLabel |
String |
該文本的另一種語(yǔ)義標(biāo)簽 |
softWrap |
bool |
文本是否應(yīng)該在換行符處斷行 |
strutStyle |
StrutStyle |
支撐樣式行嗤。Strut 樣式定義了設(shè)置最小垂直布局指標(biāo)的支持。 |
style |
TextStyle |
設(shè)置文本樣式 |
textAlign |
TextAlign |
文本水平對(duì)齊方式 |
textDirection |
TextDirection |
文本顯示的方向 |
textScaleFactor |
double |
每個(gè)邏輯像素的字體像素值 |
textSpan |
TextSpan |
以TextSpan 方式顯示文本 |
hashCode |
int |
哈希碼 |
runtimeType |
Type |
對(duì)象運(yùn)行時(shí)類型的表示形式 |
方法:
方法名 | 類型 | 說(shuō)明 |
---|---|---|
build(BuildContext context) |
Widget |
組件表示的部分用戶界面 |
debugFillProperties(DiagnosticPropertiesBulider properties) |
void |
添加與節(jié)點(diǎn)關(guān)聯(lián)的其他屬性 |
createElement |
StatelessElement |
創(chuàng)建一個(gè)StatelessElement 來(lái)管理組件在樹(shù)中的位置 |
debugDescribeChildren() |
List<DiagnosticsNode> |
返回描述詞節(jié)點(diǎn)子節(jié)點(diǎn)的diagnostics 節(jié)點(diǎn)對(duì)象列表 |
noSuchMethod(Invocation invocation) |
dynamic |
當(dāng)訪問(wèn)不存在的方法或?qū)傩詴r(shí)調(diào)用 |
toDiagnosticsNode({String name, DiagnosticsTreeStyle style}) |
DiagnosticsNode |
返回由使用調(diào)試工具和DiagnostisNode.toStringDep的對(duì)象的調(diào)試表示形式 |
toString({DiagnosticLevel minLevel: DiagnosticLevel.debug}) |
String |
返回此對(duì)象的字符串表示形式 |
toStringDeep({String prefixLineOne:'', String prefixOtherLines, DiagnosticLevel minLevel: DiagnosticLevel.debug}) |
String |
返回子節(jié)點(diǎn)和它的子節(jié)點(diǎn)的字符串表示形式 |
toStringShallow({Sting joiner:',', DiagnosticLevel minLevel:DiagnosticLevel.debug}) |
String |
返回對(duì)象的一行詳細(xì)說(shuō)明 |
toStringShort() |
String |
對(duì)組件簡(jiǎn)短的文本描述 |
示例:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
const data = "Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep";
return MaterialApp(
title: 'Hello World!',
theme: ThemeData(
primaryColor: Colors.red,
),
home: Scaffold(
appBar: AppBar(
title: Text('Welcome to Fultter'),
),
body: Center(
child: Text(
data,
maxLines: 1,
overflow: TextOverflow.ellipsis,
softWrap: true,
strutStyle: StrutStyle(),
style: TextStyle(
fontStyle: FontStyle.normal,
fontWeight: FontWeight.bold,
fontSize: 15.0
),
textAlign: TextAlign.justify,
textDirection: TextDirection.rtl,
textScaleFactor: 2.0,
),
),
),
);
}
}
參考文檔:Text class