構(gòu)造方法
const Text(this.data, {
Key key,
this.style,
this.textAlign,
this.textDirection,
this.locale,
this.softWrap,
this.overflow,
this.textScaleFactor,
this.maxLines,
this.semanticsLabel,
}
const Text.rich(this.textSpan, {
Key key,
this.style,
this.textAlign,
this.textDirection,
this.locale,
this.softWrap,
this.overflow,
this.textScaleFactor,
this.maxLines,
this.semanticsLabel,
}
- TextStyle樣式說明:
Text(
"Text組件的使用",
style: TextStyle(
// 文字顏色
color: Color(0xfff0000),
// none 不顯示裝飾線條,underline 字體下方,overline 字體上方,lineThrough穿過文字
decoration: TextDecoration.none,
// solid 直線,double 雙下劃線,dotted 虛線,dashed 點下劃線,wavy 波浪線
decorationStyle: TextDecorationStyle.solid,
// 裝飾線的顏色
decorationColor: Colors.red,
// 文字大小
fontSize: 15.0,
// normal 正常,italic 斜體
fontStyle: FontStyle.normal,
// 字體的粗細
fontWeight: FontWeight.bold,
// 文字間的寬度
letterSpacing: 1.0,
//用paint來渲染text,也可以用他來改變字體顏色等
foreground,
//單詞間距
wordSpacing,
// 文本行與行的高度,作為字體大小的倍數(shù)(取值1~2,如1.2)
height: 1,
//對齊文本的水平線:
//TextBaseline.alphabetic:文本基線是標準的字母基線
//TextBaseline.ideographic:文字基線是表意字基線;
//如果字符本身超出了alphabetic 基線,那么ideograhpic基線位置在字符本身的底部。
textBaseline: TextBaseline.alphabetic),
// 段落的間距樣式
strutStyle: StrutStyle(
fontFamily: 'serif',
fontFamilyFallback: ['monospace', 'serif'],
fontSize: 20,
height: 2,
leading: 2.0,
fontWeight: FontWeight.w300,
fontStyle: FontStyle.normal,
forceStrutHeight: true,
debugLabel: 'text demo',
),
// 文字對齊方式
textAlign: TextAlign.center,
// 文字排列方向 ltr 左到右,rtl右到左
textDirection: TextDirection.ltr,
// 【國際化】用于選擇區(qū)域特定字形的語言環(huán)境
locale: Locale('zh_CN'),
// 文本是否能換行,bool類型
softWrap: false,
// 如何處理視覺溢出:clip 剪切溢出的文本以修復其容器。ellipsis 使用省略號表示文本已溢出。fade 將溢出的文本淡化為透明。
overflow: TextOverflow.clip,
// 文字的縮放比例
textScaleFactor: 1.0,
// 文本要跨越的可選最大行數(shù),
maxLines: 2,
// 圖像的語義描述,用于向Andoid上的TalkBack和iOS上的VoiceOver提供圖像描述
semanticsLabel: 'text demo',
textWidthBasis: TextWidthBasis.longestLine,
)
- 關于Text.rich()的使用:
RichText 是 Flutter 富文本的 widget,但是 RichText 只負責 layout,具體的配置還要看 Flutter 提供的2個類型 span:TextSpan、WidgetSpan
TextSpan - 配合 textStyle 實現(xiàn)各種文字效果,可以添加點擊事件
WidgetSpan - 可以添加其他類型的 widget
return Scaffold(
appBar: AppBar(
title: Text('RichText'),
),
body: RichText(
text: TextSpan(
text: '登陸即同意',
style: TextStyle(color: Colors.black),
children: [
WidgetSpan(
alignment: PlaceholderAlignment.middle,
child: Image.asset(
AssetPathConstant.imageScan,
width: 40,
height: 40,
),
),
TextSpan(
text: '《服務條款》',
style: TextStyle(color: Colors.red),
recognizer: TapGestureRecognizer()
..onTap = () {
ToastUtil.showToast('服務條款');
},
),
TextSpan(
text: '和',
style: TextStyle(color: Colors.black),
),
WidgetSpan(
alignment: PlaceholderAlignment.bottom,
child: Image.asset(
AssetPathConstant.imageScan,
width: 40,
height: 40,
),
),
TextSpan(
text: '《隱私政策》',
style: TextStyle(color: Colors.red),
recognizer: TapGestureRecognizer()
..onTap = () {
ToastUtil.showToast('隱私政策');
},
),
],
),
),
);