TextSpan
不可变的文本范围。一个[TextSpan]对象可以使用它的[style]属性设置样式。该样式将应用于[text]和[children]。一个[TextSpan]对象可以只有纯文本,或者它可以有子[TextSpan]对象与他们自己的样式(可能只是部分)覆盖该对象的[样式]。如果[TextSpan]同时具有[text]和[children],那么[text]在[children]列表的开始处被视为一个无样式的[TextSpan]。(TextSpan离开。text]字段null将导致[TextSpan]充当具有li的[InlineSpan]树中的一个空节点。
基本属性
const TextSpan({
this.text, // 文本 如果[text]和[children]都是非空的,则文本将位于这些子元素的前面
this.children, // 文本 如果[text]和[children]都是非空的,则文本将位于这些子元素的后面
TextStyle style, // 设置样式
this.recognizer, // 手势操作 先了解
this.semanticsLabel, // 这个[TextSpan]的另一个语义标签。如果存在,此跨度的语义将包含此值,而不是实际的文本。
}) : super(style: style,);
- recognizer : 接收达到此范围的事件的手势识别器。[InlineSpan]本身不实现命中测试或事件分派。管理[InlineSpan]绘制的对象还负责分派事件。在呈现库中,它是[render段落]对象,它对应于小部件层中的[RichText]小部件;这些对象不会在[InlineSpan]s中冒泡事件,所以[识别器]只对直接击中[InlineSpan]的[文本]的事件有效,而不是它的任何[子元素]。
基本用法
RichText(
text: TextSpan(
// style : DefaultTextStyle.of(context).style, // 程序的默认字体样式
style: TextStyle( fontSize : 26, color : Colors.brown[600] ),
text: "小钟",
children: <InlineSpan>[
TextSpan(text: ","),
TextSpan(text : "是个认真", style: TextStyle(color : Colors.green[300]) ),
TextSpan(text: "且", style: TextStyle(color : Colors.deepOrangeAccent[100]) ),
TextSpan(
text : "有脾气的程序员!",
recognizer: TapGestureRecognizer()..onTap = (){
print("手势点击操作!");
},
)
],
// 接收达到此范围的事件的手势识别器, 需直接击中元素
recognizer: TapGestureRecognizer()..onTap = (){
print("手势点击操作,这里点击不了,因为点击的是子孩子,不存在冒泡行为");
}
)
),
RichText
基本属性
RichText({
Key key,
@required this.text,
this.textAlign = TextAlign.start, // 是否以及如何水平对齐文本。
this.textDirection, //文本的方向性。
this.softWrap = true, // 文本是否应该在软换行
this.overflow = TextOverflow.clip, // 如何处理溢出的文本
this.textScaleFactor = 1.0,
this.maxLines, // 可选的最大文本行数,
this.locale, // 当相同的Unicode字符可以根据语言环境以不同的方式呈现时,用于选择字体。很少需要设置此属性。默认情况下,它的值通过Localizations.localeOf(context)继承自封闭的应用程序。看到[RenderParagraph。获取更多信息。
this.strutStyle,
this.textWidthBasis = TextWidthBasis.parent,
this.textHeightBehavior,
}) : assert(text != null),
assert(textAlign != null),
assert(softWrap != null),
assert(overflow != null),
assert(textScaleFactor != null),
assert(maxLines == null || maxLines > 0),
assert(textWidthBasis != null),
super(key: key, children: _extractChildren(text));
- textDirection : 文本的方向性。这决定了[textAlign]值如何,比如[textAlign。开始]和[TextAlign。最终解释。这也用于消除如何呈现双向文本的歧义。例如,如果[text]是一个英语短语,后面跟着一个希伯来语短语,在[TextDirection。上下文英语短语将在它的左边和希伯来短语在它的右边,而在一个[文本方向。上下文,英语短语在右边,希伯来语短语在左边。如果有的话,默认为环境[方向性]。如果没有环境[方向性],那么这一种
- softWrap : 文本是否应该在软换行。如果为false,文本中的符号将被定位为没有限制的水平空间。
- overflow : 如何处理溢出的文本。可以通过[Text]和[RichText]传递[TextOverflow]。溢出]和[RichText。分别溢出]属性.
- textScaleFactor : 每个逻辑像素的字体像素数。例如,如果文本比例因子为1.5,文本将比指定的字体大小大50%。
- macLine : 可选的最大文本行数,必要时可换行。如果文本超过了给定的行数,它将根据[overflow]被截断。如果这是1,文本将不会换行。否则,文本将被包装在框的边缘。
textDirection 文本方向
RichText(
textAlign: TextAlign.start,
text: TextSpan(
// style : DefaultTextStyle.of(context).style, // 程序的默认字体样式
style: TextStyle( fontSize : 26, color : Colors.brown[600] ),
text: "小钟", // 主文体,在最前面显示
children: <InlineSpan>[ // 子文本
TextSpan(text: ","),
TextSpan(text : "是个认真", style: TextStyle(color : Colors.green[300]) ),
TextSpan(text: "且", style: TextStyle(color : Colors.deepOrangeAccent[100]) ),
TextSpan(
text : "有脾气的程序员!",
recognizer: TapGestureRecognizer()..onTap = (){
print("手势点击操作!");
},
),
TextSpan(
text : "TextSpan不可变的文本范围。一个[TextSpan]对象可以使用它的[style]属性设置样式。该样式将应用于[text]和[children]",
)
],
)
),
softWrap 文本是否应该在软换行
RichText(
textAlign: TextAlign.start,
softWrap: true,
text: TextSpan(
// style : DefaultTextStyle.of(context).style, // 程序的默认字体样式
style: TextStyle( fontSize : 26, color : Colors.brown[600] ),
text: "小钟", // 主文体,在最前面显示
children: <InlineSpan>[ // 子文本
TextSpan(text: ","),
TextSpan(text : "是个认真", style: TextStyle(color : Colors.green[300]) ),
TextSpan(text: "且", style: TextStyle(color : Colors.deepOrangeAccent[100]) ),
TextSpan(
text : "有脾气的程序员!",
recognizer: TapGestureRecognizer()..onTap = (){
print("手势点击操作!");
},
),
TextSpan(
text : "TextSpan不可变的文本范围。一个[TextSpan]对象可以使用它的[style]属性设置样式。该样式将应用于[text]和[children]",
)
],
)
),
overflow 如何处理溢出的文本
RichText(
textAlign: TextAlign.start,
softWrap: true,
overflow: TextOverflow.ellipsis,
// maxLines : 2,
text: TextSpan(
// style : DefaultTextStyle.of(context).style, // 程序的默认字体样式
style: TextStyle( fontSize : 26, color : Colors.brown[600] ),
text: "小钟", // 主文体,在最前面显示
children: <InlineSpan>[ // 子文本
TextSpan(text: ","),
TextSpan(text : "是个认真", style: TextStyle(color : Colors.green[300]) ),
TextSpan(text: "且", style: TextStyle(color : Colors.deepOrangeAccent[100]) ),
TextSpan(
text : "有脾气的程序员!",
recognizer: TapGestureRecognizer()..onTap = (){
print("手势点击操作!");
},
),
TextSpan(
text : "TextSpan不可变的文本范围。一个[TextSpan]对象可以使用它的[style]属性设置样式。该样式将应用于[text]和[children]",
)
],
)
),
textScaleFactor 每个逻辑像素的字体像素数
RichText(
textAlign: TextAlign.start,
textDirection: TextDirection.ltr,
textScaleFactor: 1.0,
// maxLines : 2,
text: TextSpan(
// style : DefaultTextStyle.of(context).style, // 程序的默认字体样式
style: TextStyle( fontSize : 26, color : Colors.brown[600] ),
text: "小钟", // 主文体,在最前面显示
children: <InlineSpan>[ // 子文本
TextSpan(text: ","),
TextSpan(text : "是个认真", style: TextStyle(color : Colors.green[300]) ),
TextSpan(text: "且", style: TextStyle(color : Colors.deepOrangeAccent[100]) ),
TextSpan(
text : "有脾气的程序员!",
recognizer: TapGestureRecognizer()..onTap = (){
print("手势点击操作!");
},
),
TextSpan(
text : "TextSpan不可变的文本范围。一个[TextSpan]对象可以使用它的[style]属性设置样式。该样式将应用于[text]和[children]",
)
],
)
),
macLine 可选的最大文本行数
RichText(
textAlign: TextAlign.start,
textDirection: TextDirection.ltr,
overflow: TextOverflow.ellipsis,
maxLines : 2,
text: TextSpan(
// style : DefaultTextStyle.of(context).style, // 程序的默认字体样式
style: TextStyle( fontSize : 26, color : Colors.brown[600] ),
text: "小钟", // 主文体,在最前面显示
children: <InlineSpan>[ // 子文本
TextSpan(text: ","),
TextSpan(text : "是个认真", style: TextStyle(color : Colors.green[300]) ),
TextSpan(text: "且", style: TextStyle(color : Colors.deepOrangeAccent[100]) ),
TextSpan(
text : "有脾气的程序员!",
recognizer: TapGestureRecognizer()..onTap = (){
print("手势点击操作!");
},
),
TextSpan(
text : "TextSpan不可变的文本范围。一个[TextSpan]对象可以使用它的[style]属性设置样式。该样式将应用于[text]和[children]",
)
],
)
),
示例
RichText(
textAlign: TextAlign.start,
textDirection: TextDirection.ltr,
softWrap: true,
overflow: TextOverflow.ellipsis,
textScaleFactor: 1.0,
maxLines : 2,
text: TextSpan(
// style : DefaultTextStyle.of(context).style, // 程序的默认字体样式
style: TextStyle( fontSize : 26, color : Colors.black ),
text: "请阅读小种条约!", // 主文体,在最前面显示
children: <InlineSpan>[ // 子文本
TextSpan(
text : "《条约明细》",
style: TextStyle(color : Colors.blue[300]),
recognizer: TapGestureRecognizer()..onTap = (){
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text('小种条约'),
content: Text('小种条约制定于202.09.24'),
actions: <Widget>[
FlatButton(child: Text('取消'),onPressed: (){},),
FlatButton(child: Text('确认'),onPressed: (){},),
],
);
});
},
),
],
)
),